C++未知类型错误

问题描述:

出于某种原因,我的main.cpp不识别链接,它通过link.h文件包含并在link.cpp中定义。是否有可能makefile没有正确地链接它们,否则afaik我应该在link.cpp和main.cpp中包含link.h,并且都可以将Link识别为一个类。C++未知类型错误

的main.cpp

#include <iostream> 
#include <link.h> 

int main (int argc, const char * argv[]) { 

int i = 10; 
while(i >= 0) { 

    i--; 
}  

Link test = new Link(); 

std::cout << "Hello, World!2\n"; 
return 42; 
} 

link.h

#ifndef LINK_H 
#define LINK_H 
#include <string> 
using namespace std; 

class Link { 
private: 
    string * value; 
    Link * next; 
public: 
    Link(Link * nextLink, string * stringValue); 
    ~Link(); 

} 
#endif 

link.cpp

#include <link.h> 

Link::Link(Link * nextLink, string * stringValue) { 

this.next = nextLink; 
this.value = stringValue; 
} 

Link::~Link() { 

delete value; 
} 

Link * Link::getNext() { 

return next; 
} 

string * Link::getString() { 

return value; 
} 

void Link::printAll() { 

if (next != NULL) { 
    cout << value << "\n" << next->printAll(); 
} else { 
    cout << value << "\n"; 
} 
} 
+0

你是如何在你的makefile联系起来? –

+3

您的链接类似乎缺少结束分号。 –

我建议你尝试,包括代替<和link.h使用双引号, >;使用这些指示编译器将文件查找到系统默认包含第一,而双引号将使它看起来在您当前的目录中,我认为你已经把它放在。

所以,你的代码将改变,无论你引用你的头:

#include "link.h"