gpt4 book ai didi

c++ - Makefile C++继承

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:37 27 4
gpt4 key购买 nike

以下是涉及的文件和简短的描述:

arrayListType.harrayListTypeImp.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

两个版本都编译arrayListTypeImp.o 并在编译unorderedArrayListTypeImp.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 scope

arrayListType.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;
};

#endif

unorderArrayListTypeImp.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

最佳答案

你没有#include <iostream>在 arrayListType.h 中,但在 arrayListType.cpp 中完成,在你之前 #include "arrayListType.h"那里。您需要放置 #include <iostream>在使用 std::cout 或 std::endl 之前进入 arrayListType.h。

为避免此类错误,最好将接口(interface) header 放在实现文件中的第一个#include 语句中。

关于c++ - Makefile C++继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6463701/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com