以下是所涉及的文件和简短描述:
arrayListType.h,arrayListTypeImp.cpp:声明并实现arrayListType类及其函数。
unorderedarrayListType.h unorderedArrayListTypeImp.cpp:继承arrayListType类,声明unorderedarrayListType类,实现arrayListType类的虚函数。
Ch13_Ex6.cpp:实例化unorderedArrayListType类的对象并运行一些测试。
我有一个编译错误,我认为这是由于Makefile。以下是错误:
unorderedArrayListTypeImp.cpp:4: error: expected unqualified-id before 'using'
unorderedArrayListTypeImp.cpp: In member function 'virtual void unorderedArrayListType::insertAt(int, int)':
unorderedArrayListTypeImp.cpp:11: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:11: error: 'endl' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'cout' was not declared in this scope第4行有一个using namespace std;命令。这之前的一行是#include "arrayListType.h"。我尝试了Makefile中的以下变体,但都不起作用:
版本1
all: Ch13_Ex6
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: arrayListTypeImp.o unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall arrayListTypeImp.o unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
Ch13_Ex6: arrayListTypeImp.o unorderedArrayListTypeImp.o Ch13_Ex6.o
g++ -Wall Ch13_Ex6.o arrayListTypeImp.o unorderedArrayListTypeImp.o -o Ch13_Ex6版本2:
all: Ch13_Ex6
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
Ch13_Ex6: arrayListTypeImp.o unorderedArrayListTypeImp.o Ch13_Ex6.o
g++ -Wall Ch13_Ex6.o arrayListTypeImp.o unorderedArrayListTypeImp.o -o Ch13_Ex6编译unorderedArrayListTypeImp.o时,两个版本都会编译arrayListTypeImp.o并给出上面所示的错误。以下是完整的编译输出:
make
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
arrayListTypeImp.o
unorderedArrayListType.h:16: error: expected unqualified-id at end of input
unorderedArrayListTypeImp.cpp:4: error: expected unqualified-id before 'using'
unorderedArrayListTypeImp.cpp: In member function 'virtual void unorderedArrayListType::insertAt(int, int)':
unorderedArrayListTypeImp.cpp:11: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:11: error: 'endl' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'endl' was not declared in this scopeArrayListType.h的代码:
#ifndef H_arrayListType
#define H_arrayListType
class arrayListType
{
public:
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, int item) const;
virtual void insertAt(int location, int insertItem) = 0;
virtual void insertEnd(int insertItem) = 0;
void removeAt(int location);
int retrieveAt(int location) const;
virtual void replaceAt(int location, int repItem) = 0;
void clearList();
virtual int seqSearch(int searchItem) const = 0;
virtual void remove(int removeItem) = 0;
arrayListType(int size = 100);
arrayListType(const arrayListType& otherList);
virtual ~arrayListType();
protected:
int *list;
int length;
int maxSize;
};
#endifUnorderArrayListTypeImp.cpp代码:
#include <iostream>
#include "unorderedArrayListType.h"
using namespace std;
void unorderedArrayListType::insertAt(int location,
int insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at
//the specified position
length++; //increment the length
}
} //end insertAt
void unorderedArrayListType::insertEnd(int insertItem)
{
if (length >= maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
// More virtual functions implemented and finally a constructor
unorderedArrayListType::unorderedArrayListType(int size)
: arrayListType(size)
{
} //end constructor发布于 2011-06-24 13:51:39
你没有在arrayListType.h中使用#include <iostream>,而是在arrayListType.cpp中使用#include "arrayListType.h"。在使用std::cout或std::endl之前,您需要将#include <iostream>放入arrayListType.h中。
为了避免这样的错误,最好将接口头作为第一个#include语句放在实现文件中。
发布于 2011-06-24 13:19:25
我猜错误是在unorderedArrayListType.h中,你可能遗漏了一个分号之类的东西。查看Makefile并不能解决这个错误。
编辑:哇,你的Makefile确实有问题!嘿,我刚刚看了一下,你看到了以下内容:
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp不要将.h文件传递给g++!,只传递.cpp文件。所以要这样写:
g++ -c -Wall arrayListTypeImp.cpp同样:
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp应该是:
g++ -c -Wall unorderedArrayListTypeImp.cpp发布于 2011-06-24 13:51:54
埃文·特兰可能是对的。expected unqualified-id before '...'通常表示在该行之前漏掉了一个分号。如果它真的是你的makefile,这里有一些通用的makefile建议:
g++ -c -Wall ...
你可以做到
CXX_OPTS= -Wall -O3 ...g++ -c $(CXX_OPTS) ...
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp g++ -c -Wall arrayListType.h arrayListTypeImp.cpp unorderedArrayListTypeImp.o: unorderedArrayListType.h unorderedArrayListTypeImp.cpp g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp Ch13_Ex6.o: Ch13_Ex6.cpp g++ -c -Wall Ch13_Ex6.cpp
您可以使用
%.o:%.cpp g++ $(CXX_OPTS) -c $< -o $@
这将缩短您的makefile,并使其更容易调试和发现您的问题。如果您想了解更多信息,请查看this。
https://stackoverflow.com/questions/6463701
复制相似问题