简体   繁体   English

Makefile C ++继承

[英]Makefile C++ inheritance

The following are the files involved and a short description: 以下是涉及的文件和简短描述:

arrayListType.h , arrayListTypeImp.cpp : declare and implement arrayListType class and its functions. arrayListType.harrayListTypeImp.cpp :声明并实现arrayListType类及其功能。

unorderedarrayListType.h unorderedArrayListTypeImp.cpp : inherit arrayListType class and declare unorderedarrayListType class and implement virtual functions of arrayListType class. unorderedarrayListType.h unorderedArrayListTypeImp.cpp :继承arrayListType类并声明unorderedarrayListType类,并实现arrayListType类的虚函数。

Ch13_Ex6.cpp : Instantiates an object of class unorderedArrayListType and runs some tests. Ch13_Ex6.cpp :实例化类unorderedArrayListType的对象并运行一些测试。

I am having a compilation error, which I think is due to the Makefile. 我遇到编译错误,我认为是由于Makefile引起的。 The following is the error: 以下是错误:

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

Line 4 has a using namespace std; 第4行有一个using namespace std; command. 命令。 The line before that is #include "arrayListType.h" . 在此之前的行是#include "arrayListType.h" I have tried the following variations in the Makefile but neither worked: 我已经在Makefile中尝试了以下变体,但是都没有用:

Version 1 版本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

Version 2: 版本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

Both versions compile arrayListTypeImp.o and give the error shown above when compiling unorderedArrayListTypeImp.o . 这两个版本都编译arrayListTypeImp.o并在编译unorderedArrayListTypeImp.o时给出上面显示的错误。 The following is the complete compile output: 以下是完整的编译输出:

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

Code for arrayListType.h: 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

Code for unorderArrayListTypeImp.cpp: 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

I am guessing that the error is in unorderedArrayListType.h , you likely have a missing semicolon or something. 我猜错误是在unorderedArrayListType.h ,您可能缺少分号或其他内容。 Looking in the Makefile will do nothing to solve that error. 查看Makefile将无法解决该错误。

EDIT: Woah, there actually is something wrong with your Makefile! 编辑:哇,您的Makefile实际上有问题! Heh, I just looked at it and you have the following: 嘿,我刚看了一下,您就会看到以下内容:

g++ -c -Wall arrayListType.h arrayListTypeImp.cpp

Don't pass .h files to g++!, only the .cpp files. 不要将.h文件传递给g ++ !,而只能传递.cpp文件。 So write it like this: 所以这样写:

g++ -c -Wall arrayListTypeImp.cpp

Likewise: 同样地:

g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp

should be: 应该:

g++ -c -Wall unorderedArrayListTypeImp.cpp

You did not #include <iostream> in arrayListType.h, but did it in arrayListType.cpp, before you #include "arrayListType.h" there. 您没有#include <iostream>在arrayListType.h中,但是在arrayListType.cpp中做到了,然后在其中#include "arrayListType.h" You need to place #include <iostream> into arrayListType.h before you use std::cout or std::endl. 在使用std :: cout或std :: endl之前,需要将#include <iostream>放入arrayListType.h中。

To avoid such mistakes it is good to place the interface header as the first #include statement int the implementation file. 为避免此类错误,最好将接口标头放在实现文件中的第一个#include语句中。

Evan Teran is probably right. 埃文·特兰(Evan Teran)可能是对的。 expected unqualified-id before '...' usually means you missed a semicolon before that line. expected unqualified-id before '...'通常意味着您在该行之前错过了分号。 If it turns out it really is your makefile, here's some general makefile advice: 如果事实证明它确实是您的makefile,那么这里是一些通用的makefile建议:

  1. Use variables. 使用变量。 Instead of 代替

     g++ -c -Wall ... 

    you can do 你可以做

     CXX_OPTS= -Wall -O3 ... g++ -c $(CXX_OPTS) ... 
  2. Use pattern rules. 使用模式规则。 Rather than 而不是

     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 

    you can use 您可以使用

     %.o:%.cpp g++ $(CXX_OPTS) -c $< -o $@ 

This should shorten your makefile and make it easier to debug and find your problem. 这将缩短您的makefile,并使调试和查找问题更加容易。 If you want more information, check this out. 如果您需要更多信息,请查看信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM