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.
unorderedarrayListType.h
unorderedArrayListTypeImp.cpp
: inherit arrayListType
class and declare unorderedarrayListType
class and implement virtual functions of arrayListType
class.
Ch13_Ex6.cpp
: Instantiates an object of class unorderedArrayListType
and runs some tests.
I am having a compilation error, which I think is due to the Makefile开发者_StackOverflow. 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;
command. The line before that is #include "arrayListType.h"
. I have tried the following variations in the Makefile but neither worked:
Version 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:
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
. 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:
#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:
#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
You did not #include <iostream>
in arrayListType.h, but did it in arrayListType.cpp, before you #include "arrayListType.h"
there. You need to place #include <iostream>
into arrayListType.h before you use std::cout or std::endl.
To avoid such mistakes it is good to place the interface header as the first #include statement int the implementation file.
I am guessing that the error is in unorderedArrayListType.h
, you likely have a missing semicolon or something. Looking in the Makefile will do nothing to solve that error.
EDIT: Woah, there actually is something wrong with your 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. 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
Evan Teran is probably right. expected unqualified-id before '...'
usually means you missed a semicolon before that line. If it turns out it really is your makefile, here's some general makefile advice:
Use variables. Instead of
g++ -c -Wall ...
you can do
CXX_OPTS= -Wall -O3 ... g++ -c $(CXX_OPTS) ...
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. If you want more information, check this out.
精彩评论