我发现了很多关于这些错误的问题,但没有任何帮助我。这是我的makefile,main函数文件和main中使用的类文件。我知道这是一个链接问题但无法找到我的错误。任何帮助表示赞赏!
编辑:抱歉......这是错误消息。我很紧张,忘了把它包括在内。
prog1.o: In function `main':
prog1.cpp:(.text+0x23): undefined reference to `Quash::Quash()'
prog1.cpp:(.text+0x89): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0xa3): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0xde): undefined reference to `Quash::insert(int)'
prog1.cpp:(.text+0x143): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x15d): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x1d7): undefined reference to `Quash::empty()'
prog1.cpp:(.text+0x208): undefined reference to `Quash::root()'
prog1.cpp:(.text+0x21c): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x231): undefined reference to `Quash::deleteMin()'
prog1.cpp:(.text+0x280): undefined reference to `Quash::deleteNum(int)'
prog1.cpp:(.text+0x30b): undefined reference to `Quash::deleteNum(int)'
prog1.cpp:(.text+0x36d): undefined reference to `Quash::contains(int)'
prog1.cpp:(.text+0x3bd): undefined reference to `Quash::print()'
collect2: error: ld returned 1 exit status
生成文件:
all: prog1
prog1: Quash.o prog1.o
g++ *.o -o prog1
prog1.o: prog1.cpp
g++ -c prog1.cpp
Quash.o: Quash.h Quash.cpp Hashtable.o Minheap.o Node.o
g++ -c Quash.cpp
Hashtable.o: Hashtable.h Hashtable.cpp
g++ -c Hashtable.cpp
Minheap.o: Minheap.h Minheap.cpp
g++ -c Minheap.cpp
Node.o: Node.h Node.cpp
g++ -c Node.cpp
clean:
rm -f *.o
rm -f prog1
prog1.cpp:
#include "Quash.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
Quash* theQuash = new Quash();
while(!cin.eof())
{
string cmd;
cin >> cmd;
if(cmd.compare("insert") == 0)
{
int i;
cin >> i;
if(theQuash->contains(i) != 0)
{
cout << "item already present, new count = " << theQuash->contains(i) << endl;
}
else
{
if(theQuash->insert(i))
{
cout << "item successfully inserted, count = 1" << endl;
}
}
}
else if(cmd.compare("lookup") == 0)
{
int i;
cin >> i;
if(theQuash->contains(i) != 0)
{
cout << "item found, count = " << theQuash->contains(i) << endl;
}
else
{
cout << "item not found" << endl;
}
}
else if(cmd.compare("deleteMin") == 0)
{
if(theQuash->empty())
{
cout << "min item not present since table is empty" << endl;
}
else
{
int i = theQuash->root(), retVal = theQuash->contains(i);
if(retVal == 1)
{
theQuash->deleteMin();
cout << "min item " << i << " successfully deleted" << endl;
}
else if(retVal > 1)
{
int newCount = theQuash->deleteNum(i);
cout << "min item = " << i << ", count decremented, new count = " << newCount << endl;
}
}
}
else if(cmd.compare("delete") == 0)
{
int i;
cin >> i;
int retVal = theQuash->deleteNum(i);
if(retVal == 1)
{
cout << "item successfully deleted" << endl;
}
else if(retVal == 2)
{
cout << "item not present in the table" << endl;
}
else if(retVal == 0)
{
cout << "item count decremented, new count = " << theQuash->contains(i) << endl;
}
}
else if(cmd.compare("print") == 0)
{
theQuash->print();
}
}
}
Quash.h
#ifndef QUASH_H
#define QUASH_H
#include "Hashtable.h"
#include "Minheap.h"
class Quash{
private:
Minheap heap;
Hashtable hash;
public:
Quash();
~Quash();
int root();
bool empty();
bool insert(int i);
bool lookup(int i);
bool deleteMin();
int deleteNum(int i);
void print();
int contains(int i);
};
#endif
Quash.cpp:
#include "Hashtable.h"
#include "Minheap.h"
#include <iostream>
using namespace std;
class Quash
{
private:
Minheap* heap;
Hashtable* hash;
public:
Quash()
{
heap = new Minheap();
hash = new Hashtable();
}
~Quash()
{
delete heap;
delete hash;
}
int root()
{
return heap->getRoot();
}
bool empty()
{
if(heap->getNumElements() == 0)
{
return true;
}
else
{
return false;
}
}
bool insert(int i)
{
Node* temp = new Node(i);
if(heap->insert(temp) && hash->insert(*temp))
{
return true;
}
else
{
delete temp;
return false;
}
}
bool deleteMin()
{
return false;
}
int deleteNum(int i)
{
Node* temp = new Node(i);
int retVal = hash->deleteNode(*temp);
/*TODO
HEAP DELETION
*/
delete temp;
return retVal;
}
void print()
{
heap->print();
return;
}
int contains(int i) // lookup(i) in assignment
{
Node* temp = new Node(i);
int retVal = hash->lookup(*temp);
delete temp;
if(retVal == 0)
{
return 0;
}
else
{
return retVal;
}
}
};
答案 0 :(得分:1)
您需要将定义(在Quash.h
中)与实现(在Quash.cpp
中)
Quash.h
#ifndef QUASH_H_
#define QUASH_H_ // protect against multiple include
class Quash
{
Quash(); // constructor, declare only the prototype
// similarly for the rest of the methods
};
#endif
Quash.cpp
#include "Quash.h"
#include "Hashtable.h"
#include "Minheap.h"
// DO NOT redefine your class here, only implement its methods
Quash::Quash()
{
// now implement the constructor
heap = new Minheap();
hash = new Hashtable();
};
Quash::~Quash()
{
// and the destructor
delete heap;
delete hash;
}
// do the same for the rest of the methods
并且,main.cpp
,#include "Quash.h"
答案 1 :(得分:1)
由于某些文件内容在问题的前几个编辑中被错误标记,看起来有点混乱。基于发布的内容和思想者的错误消息,看起来发生的事情是,您有两个单独的class Queue
声明(一个在Queue.h
中,另一个在Queue.cpp
中。 1}}除了编译器看到的内联函数之外什么都没用。
您应该执行以下操作之一:
class Quash
中的Quash.cpp
声明移至Quash.h
标题中,以便函数内嵌并可供class Quash
所有客户使用更改Quash.cpp
,以便#include "Quash.h
获取class Quash
的声明并简单地实现这些功能。他们最终看起来像:
#include <iostream>
#include "Hashtable.h"
#include "Minheap.h"
#include "Quash.h"
// note: there is no class Quash { ... } surrounding these functions -
// the class has already been declared in Quash.h, the .cpp file
// only contains the function defintions for any functions that
// are not defined inline in the .h file (and definitions of
// static data members)
Quash::Quash()
{
heap = new Minheap();
hash = new Hashtable();
}
Quash::~Quash()
{
delete heap;
delete hash;
}
// etc for each of the functions delcared (but not inline-defined) in `Quash.h`
答案 2 :(得分:1)
在Quash.ccp中,您再次声明该类。
您应该改为定义方法。例如:
Quash::Quash()
{
heap = new Minheap();
hash = new Hashtable();
}
在stackoverflow上,您应该发布最少的代码,显示您遇到的问题(这是一个很好的练习,在保持错误的同时缩小代码),我觉得您没有这样做,所以我&#39;我想知道你在做什么,以防止复制建设和任务。