首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Makefile C++继承

Makefile C++继承
EN

Stack Overflow用户
提问于 2011-06-24 13:15:36
回答 3查看 3.9K关注 0票数 1

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

arrayListType.harrayListTypeImp.cpp:声明并实现arrayListType类及其函数。

unorderedarrayListType.h unorderedArrayListTypeImp.cpp:继承arrayListType类,声明unorderedarrayListType类,实现arrayListType类的虚函数。

Ch13_Ex6.cpp:实例化unorderedArrayListType类的对象并运行一些测试。

我有一个编译错误,我认为这是由于Makefile。以下是错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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并给出上面所示的错误。以下是完整的编译输出:

代码语言:javascript
复制
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的代码:

代码语言:javascript
复制
#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代码:

代码语言:javascript
复制
#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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-24 13:51:39

你没有在arrayListType.h中使用#include <iostream>,而是在arrayListType.cpp中使用#include "arrayListType.h"。在使用std::cout或std::endl之前,您需要将#include <iostream>放入arrayListType.h中。

为了避免这样的错误,最好将接口头作为第一个#include语句放在实现文件中。

票数 3
EN

Stack Overflow用户

发布于 2011-06-24 13:19:25

我猜错误是在unorderedArrayListType.h中,你可能遗漏了一个分号之类的东西。查看Makefile并不能解决这个错误。

编辑:哇,你的Makefile确实有问题!嘿,我刚刚看了一下,你看到了以下内容:

代码语言:javascript
复制
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp

不要将.h文件传递给g++!,只传递.cpp文件。所以要这样写:

代码语言:javascript
复制
g++ -c -Wall arrayListTypeImp.cpp

同样:

代码语言:javascript
复制
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp

应该是:

代码语言:javascript
复制
g++ -c -Wall unorderedArrayListTypeImp.cpp
票数 3
EN

Stack Overflow用户

发布于 2011-06-24 13:51:54

埃文·特兰可能是对的。expected unqualified-id before '...'通常表示在该行之前漏掉了一个分号。如果它真的是你的makefile,这里有一些通用的makefile建议:

  1. 使用变量。而不是

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6463701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档