SortedArray 程序未编译

Posted

技术标签:

【中文标题】SortedArray 程序未编译【英文标题】:SortedArray program not compiling 【发布时间】:2020-05-06 05:16:08 【问题描述】:

我目前在编译我的 c++ SortedArray 程序时遇到问题。我真的不知道我的程序哪里出错了,我想找出问题所在。如果有人可以帮助我,那就太好了。我想知道是否可能由于指针而错误地调用了我的函数,但尝试使用箭头和点运算符。这是我的源代码

编译错误

In file included from SortedArray.cpp:1:0:
SortedArray.h:17:12: error: ‘ostream’ does not name a type
     friend ostream & operator (ostream & out, const SortedArray & sA);
            ^~~~~~~
SortedArray.cpp: In member function ‘void SortedArray::insertVal(int)’:
SortedArray.cpp:57:12: error: request for member ‘isFull’ in ‘((SortedArray*)this)->SortedArray::arr’, which is of non-class type ‘int*’
     if(arr.isFull())
            ^~~~~~
SortedArray.cpp:58:11: error: request for member ‘expand’ in ‘((SortedArray*)this)->SortedArray::arr’, which is of non-class type ‘int*’
       arr.expand();
           ^~~~~~
SortedArray.cpp: In member function ‘bool SortedArray::deleteVal(int)’:
SortedArray.cpp:86:17: error: request for member ‘find’ in ‘((SortedArray*)this)->SortedArray::arr’, which is of non-class type ‘int*’
   int index=arr.find(val);
                 ^~~~
SortedArray.cpp: At global scope:
SortedArray.cpp:97:2: error: ‘ostream’ does not name a type
  ostream & operator<<(ostream &out, const SortedArray & sA)
  ^~~~~~~

//SortedArray.h

#ifndef SORTEDARRAY_H_
#define SORTEDARRAY_H_

class SortedArray

  public:
    SortedArray(int cap=10);
    ~SortedArray();
    int getCapacity();
    int getSize();
    bool isFull();
    bool isEmpty();
    void expand();
    void insertVal(int val);
    int find(int val);
    bool deleteVal(int val);
    friend ostream & operator (ostream & out, const SortedArray & sA);

  private:
    int capacity;
    int size;
    int *arr;
;
#endif

//SortedArray.cpp

#include "SortedArray.h"

SortedArray::SortedArray(int cap)

  capacity=cap;
  size=0;
  arr= new int[capacity];


SortedArray::~SortedArray()

  delete arr;
  size=0;

int SortedArray::getCapacity()

  return capacity;


int SortedArray::getSize()

  return size;


bool SortedArray::isFull()

  if(size==capacity);
    return true;

  return false;


bool SortedArray::isEmpty()


    if(size==0)
      return true;

    return false;


void SortedArray::expand()

    capacity=capacity*2;
    int *newArr=new int[capacity];
    for (int i=0;i<size;i++)
    
      newArr[i]=arr[i];
    
    delete arr;
    arr=newArr;


void SortedArray::insertVal(int val)

    if(arr->isFull())
      arr->expand();
    for(int i=0;i<size;i++)
    
      if(arr[i]<=val)
      
        for(int j =size;j>=i;j--)
        
          arr[j+1]=arr[j];
        
      
      arr[i]=val;
      size++;
    


int SortedArray::find(int val)

  for(int i=0;i<size;i++)
  
    if(arr[i]=val)
    
      return i;
    
  
  return -1;


bool SortedArray::deleteVal(int val)
  int index=arr->find(val);
  if(index==-1)
    return false;
  size--;
  for (int i=index;i<size;i++)
  
    arr[i]= arr[i+1];
  
  return true;


 ostream & operator<<(ostream &out, const SortedArray & sA)
  for(int i = 0; i < sA.getSize(); i++) 
        out << sA.arr[i] << " ";
    
    return out;

【问题讨论】:

error: ‘ostream’ does not name a type ... 一般表示缺少头文件。我没有看到任何内容。 另外,ostreamstd 命名空间中。 另外arrint*。它没有任何方法。您可能指的是 this-&gt;isFull() 而不是 arr-&gt;isFull() 等等。 【参考方案1】:

你有很多小问题。要开始超载&lt;&lt;,您必须#include &lt;iostream&gt;

SortedArray.h 中,您缺少&lt;&lt;

friend std::ostream& operator<< (std::ostream& out, const SortedArray& sA);

SortedArray.c 中,您有一个不起作用的if 在:

if(size==capacity);        // remove the ';'

isFull() expand()find() 不是非类类型 arr 的类成员函数。你只需要:

if (isFull())
    expand();

int index=find(val);

您对if (arr[i]=val) 的测试是比较,而不是作业,您需要:

if(arr[i] == val)

最后,为了在&lt;&lt; 的重载中传递const SortedArray&amp; sA,那么sA.getSize() 必须是常量,例如在您的标题和来源中,您需要:

int getSize() const;

int SortedArray::getSize() const

    return size;

如果我都记得它们,那么您现在应该能够在没有警告的情况下进行编译。如果您仍有问题,请添加评论,我很乐意为您提供进一步帮助。

【讨论】:

非常感谢您解决了这个问题。非常感谢你! 不客气,请看:What should I do when someone answers my question?

以上是关于SortedArray 程序未编译的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解

java Arrays.binarySearch 找不到目标

C++ 指针取消引用赋值

iPhone通讯录中的联系人排序

未发现数据源名称并且未指定默认驱动程序

[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序