将插入函数用于myList时,编译存在问题,将代码更改为其他类型(例如int)时,代码可以正常工作。插入函数是单独编译的,并与makefile链接。我刚刚包括了我遇到问题的代码部分,其余部分都很好。
错误: g ++ -o proj6Base proj6Base.o island.o
proj6Base.o:在函数ArchipelagoExpedition::insertfilename(char*)':
proj6Base.cpp:(.text._ZN21ArchipelagoExpedition14insertfilenameEPc[_ZN21ArchipelagoExpedition14insertfilenameEPc]+0x2
2): undefined reference to
myList :: insert(char *)'中
collect2:错误:ld返回1退出状态
make:*** [proj6Base]错误1
proj6base.cpp-
#include "island.h"
class ArchipelagoExpedition
{
private:
// Create the Data Members for the Archipelago Network here
island* Exp[];
myList<char*>* filenames;
public:
// Use a constructor to initialize the Data Members for your expedition
ArchipelagoExpedition()
{
island* Exp = new island[10];
myList<char*>* filenames;
}
void insertfilename(char* fname){
filenames->insert(fname);
}
}
island.cpp
#include "island.h"
template<typename T>
myList<T>::myList(){
Head = NULL;
}
template<typename T>
myList<T>::myList(T v1){
Head = new myNode<T>;
Head->setName(v1);
}
template<typename T>
void myList<T>::show(){
myNode<T> *temp = Head;
while (temp!=NULL){
printf("%d "),temp->getName();
temp->setNext(temp->getNext());
}
}
template<typename T>
void myList<T>::insert (T v1){
if (Head == NULL){
Head = new myNode<T>;
Head->setName(v1);
return;
}
myNode<T> *temp = Head;
while (temp->getNext() != NULL){
temp = temp->getNext();
}
myNode<T> *newroute = new myNode<T>;
newroute->setName(v1);
temp->setNext(newroute);
}
}
island.h
#include <cstdio>
#include <cstring>
#include <cstdlib>
template<typename T>
class myList
{
// data members
private:
// the X and Y coordinates for the point
myNode<T>* Head;
public:
// use of class constructors to initialize the variables
myList();
myList(T v1);
void show();
void insert (T v1);
}
makefile
proj6Base: proj6Base.o island.o
g++ -o proj6Base proj6Base.o island.o
proj6Base.o: proj6Base.cpp island.h
g++ -c proj6Base.cpp
island.o: island.cpp island.h
g++ -c island.cpp
clean:
rm proj6Base.o island.o proj6Base
run:
./proj6Base