C++ C+上的错误+;链表类
编辑:std::string仍然有一个错误,但我已经在每个文件上都做了#include 我有一个C++类的链表类,看起来是这样的: link.cppC++ C+上的错误+;链表类,c++,linked-list,C++,Linked List,编辑:std::string仍然有一个错误,但我已经在每个文件上都做了#include 我有一个C++类的链表类,看起来是这样的: link.cpp #include <iostream> link::link(link * pointer_to_link, string * pointer_to_string) { next = pointer_to_link; value = pointer_to_string; } link::~link() { delete value
#include <iostream>
link::link(link * pointer_to_link, string * pointer_to_string)
{
next = pointer_to_link;
value = pointer_to_string;
}
link::~link() {
delete value;
}
link * link::getNext() {
return next;
}
std::string * link::getString() {
return value;
}
int link::printAll(link link_to_print) {
std::cout << link_to_print.getString() + "\n";
if (link_to_print.next != NULL) {
printAll(link_to_print.getNext());
}
}
#包括
link::link(link*指针指向链接,string*指针指向字符串)
{
下一步=指向链接的指针;
值=指向字符串的指针;
}
链接::~link(){
删除值;
}
link*link::getNext(){
下一步返回;
}
std::string*link::getString(){
返回值;
}
int link::printAll(链接到打印){
std::cout next=新链接(空,*新字符串);
filler\u linker=头\u指针;
}否则{
填充链接->下一步=新链接(空,*新链接字符串);
head_ponter=NULL;
}
filler\u link=filler\u linker->next;
}
头指针->打印全部(头指针);
返回42;
}
非常简单,但是当我运行程序时,每当我使用或实例化链接时,我会得到大约十个不同的错误,我的终端输出如下所示:
In file included from main.cpp:1:
./link.h:6:2: error: use of undeclared identifier 'std'
std::string *value;
^
./link.h:7:8: error: C++ requires a type specifier for all declarations
link *next;
~^
./link.h:10:29: error: expected ')'
link(link * pointer_to_link, std::string * pointer_to_string);
^
./link.h:10:6: note: to match this '('
link(link * pointer_to_link, std::string * pointer_to_string);
^
./link.h:10:14: error: C++ requires a type specifier for all declarations
link(link * pointer_to_link, std::string * pointer_to_string);
~ ^
./link.h:11:7: error: expected a class name after '~' to name a destructor
~link();
^
./link.h:12:9: error: C++ requires a type specifier for all declarations
link * getNext();
~ ^
./link.h:13:2: error: use of undeclared identifier 'std'
std::string * getString();
^
main.cpp:8:10: error: use of undeclared identifier 'head_pointer'
link * head_pointer = NULL;
^
main.cpp:9:10: error: use of undeclared identifier 'filler_link'
link * filler_link = NULL;
^
main.cpp:11:2: error: use of undeclared identifier 'head_pointer'
head_pointer -> next = new link(NULL, *new_string);
^
main.cpp:11:40: error: indirection requires pointer operand ('std::string' (aka
'basic_string<char>') invalid)
head_pointer -> next = new link(NULL, *new_string);
^~~~~~~~~~~
main.cpp:12:2: error: use of undeclared identifier 'filler_linker'
filler_linker = head_pointer;
^
main.cpp:12:18: error: use of undeclared identifier 'head_pointer'
filler_linker = head_pointer;
^
main.cpp:14:2: error: use of undeclared identifier 'filler_link'
filler_link -> next = new link(NULL, *new_string);
^
main.cpp:14:39: error: indirection requires pointer operand ('std::string' (aka
'basic_string<char>') invalid)
filler_link -> next = new link(NULL, *new_string);
^~~~~~~~~~~
main.cpp:15:2: error: use of undeclared identifier 'head_ponter'
head_ponter = NULL;
^
main.cpp:17:1: error: use of undeclared identifier 'filler_link'
filler_link = filler_linker -> next;
^
main.cpp:17:15: error: use of undeclared identifier 'filler_linker'
filler_link = filler_linker -> next;
^
main.cpp:19:1: error: use of undeclared identifier 'head_pointer'
head_pointer -> printAll(head_pointer);
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
link.cpp:2:1: error: use of undeclared identifier 'link'
link::link(link * pointer_to_link, string * pointer_to_string)
^
link.cpp:2:36: error: unknown type name 'string'; did you mean 'std::string'?
link::link(link * pointer_to_link, string * pointer_to_string)
^~~~~~
std::string
/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/bits/stringfwd.h:62:33: note:
'std::string' declared here
typedef basic_string<char> string;
^
link.cpp:2:12: error: unknown type name 'link'
link::link(link * pointer_to_link, string * pointer_to_string)
^
link.cpp:7:1: error: use of undeclared identifier 'link'
link::~link() {
^
link.cpp:7:8: error: expected the class name after '~' to name a destructor
link::~link() {
^
link.cpp:10:1: error: unknown type name 'link'
link * link::getNext() {
^
link.cpp:10:8: error: use of undeclared identifier 'link'
link * link::getNext() {
^
link.cpp:13:15: error: use of undeclared identifier 'link'
std::string * link::getString() {
^
link.cpp:16:5: error: use of undeclared identifier 'link'
int link::printAll(link link_to_print) {
^
link.cpp:16:20: error: unknown type name 'link'
int link::printAll(link link_to_print) {
^
10 errors generated.
在main.cpp:1中包含的文件中:
./link.h:6:2:错误:使用未声明的标识符“std”
std::字符串*值;
^
/Link:h:7:8:错误:C++需要所有声明的类型说明符
链接*下一步;
~^
./link.h:10:29:错误:预期''
链接(链接*指针指向链接,标准::字符串*指针指向字符串);
^
./link.h:10:6:注:匹配此“(”
链接(链接*指针指向链接,标准::字符串*指针指向字符串);
^
/Link:h:10:14:错误:C++需要所有声明的类型说明符
链接(链接*指针指向链接,标准::字符串*指针指向字符串);
~ ^
./link.h:11:7:错误:应在“~”之后使用类名来命名析构函数
~link();
^
/Link:h:12:9:错误:C++需要所有声明的类型说明符
链接*getNext();
~ ^
./link.h:13:2:错误:使用未声明的标识符'std'
std::string*getString();
^
main.cpp:8:10:错误:使用未声明的标识符“head\u pointer”
link*head\u指针=NULL;
^
main.cpp:9:10:错误:使用未声明的标识符“filler\u link”
link*filler\u link=NULL;
^
main.cpp:11:2:错误:使用未声明的标识符“head\u pointer”
head\u pointer->next=新链接(空,*新字符串);
^
main.cpp:11:40:错误:间接寻址需要指针操作数('std::string'(aka
“基本字符串”)无效)
head\u pointer->next=新链接(空,*新字符串);
^~~~~~~~~~~
main.cpp:12:2:错误:使用未声明的标识符“filler\u linker”
filler\u linker=头\u指针;
^
main.cpp:12:18:错误:使用未声明的标识符“head\u pointer”
filler\u linker=头\u指针;
^
main.cpp:14:2:错误:使用未声明的标识符“filler\u link”
填充链接->下一步=新链接(空,*新链接字符串);
^
main.cpp:14:39:错误:间接寻址需要指针操作数('std::string'(aka
“基本字符串”)无效)
填充链接->下一步=新链接(空,*新链接字符串);
^~~~~~~~~~~
main.cpp:15:2:错误:使用未声明的标识符“head\u ponter”
head_ponter=NULL;
^
main.cpp:17:1:错误:使用未声明的标识符“filler\u link”
filler\u link=filler\u linker->next;
^
main.cpp:17:15:错误:使用未声明的标识符“filler\u linker”
filler\u link=filler\u linker->next;
^
main.cpp:19:1:错误:使用未声明的标识符“head\u pointer”
头指针->打印全部(头指针);
^
致命错误:发出的错误太多,正在停止[-ferror limit=]
产生了20个错误。
link.cpp:2:1:错误:使用未声明的标识符“link”
link::link(link*指针指向链接,string*指针指向字符串)
^
link.cpp:2:36:错误:未知类型名称“string”;您是指“std::string”吗?
link::link(link*指针指向链接,string*指针指向字符串)
^~~~~~
字符串
/usr/bin/./lib/gcc/x86_64-redhat-linux/4.8.5/../../../../../../../include/c++/4.8.5/bits/stringfwd.h:62:33:注:
此处声明了“std::string”
typedef基本字符串;
^
link.cpp:2:12:错误:未知类型名称“link”
link::link(link*指针指向链接,string*指针指向字符串)
^
link.cpp:7:1:错误:使用未声明的标识符“link”
链接::~link(){
^
link.cpp:7:8:错误:应在“~”之后使用类名来命名析构函数
链接::~link(){
^
link.cpp:10:1:错误:未知类型名称“link”
link*link::getNext(){
^
link.cpp:10:8:错误:使用未声明的标识符“link”
link*link::getNext(){
^
link.cpp:13:15:错误:使用未声明的标识符“link”
std::string*link::getString(){
^
link.cpp:16:5:错误:使用未声明的标识符“link”
int link::printAll(链接到打印){
^
link.cpp:16:20:错误:未知类型名称“link”
int link::printAll(链接到打印){
^
产生了10个错误。
您需要在link.h中包含。第一行告诉您,当试图声明std::string时,编译器找不到名为“std”的命名空间
此外,@Rogus在评论中指出,您还需要#在link.cpp中包含“link.h”
。这会导致link.cpp中错误列表末尾出现错误。显示link.h
文件?我投票决定以简单的打字方式关闭#包含和#包含“link.h”
在.cpp
A中包含“link.h”link.cpp
中的
也很有用啊,是的,这一点很好。我没有深入到错误列表中。我会更新。谢谢你的建议。我仍然会在链接列表中看到错误,但是请编辑你的问题,列出你正在遇到的新错误,或者指出哪些错误不是新错误。@qazwsxedcrfvtgbyhn尝试阅读编译器错误并自己解决问题。大多数消息都是直截了当的,有些甚至给出了错误的提示以及如何修复。显示一些努力。
In file included from main.cpp:1:
./link.h:6:2: error: use of undeclared identifier 'std'
std::string *value;
^
./link.h:7:8: error: C++ requires a type specifier for all declarations
link *next;
~^
./link.h:10:29: error: expected ')'
link(link * pointer_to_link, std::string * pointer_to_string);
^
./link.h:10:6: note: to match this '('
link(link * pointer_to_link, std::string * pointer_to_string);
^
./link.h:10:14: error: C++ requires a type specifier for all declarations
link(link * pointer_to_link, std::string * pointer_to_string);
~ ^
./link.h:11:7: error: expected a class name after '~' to name a destructor
~link();
^
./link.h:12:9: error: C++ requires a type specifier for all declarations
link * getNext();
~ ^
./link.h:13:2: error: use of undeclared identifier 'std'
std::string * getString();
^
main.cpp:8:10: error: use of undeclared identifier 'head_pointer'
link * head_pointer = NULL;
^
main.cpp:9:10: error: use of undeclared identifier 'filler_link'
link * filler_link = NULL;
^
main.cpp:11:2: error: use of undeclared identifier 'head_pointer'
head_pointer -> next = new link(NULL, *new_string);
^
main.cpp:11:40: error: indirection requires pointer operand ('std::string' (aka
'basic_string<char>') invalid)
head_pointer -> next = new link(NULL, *new_string);
^~~~~~~~~~~
main.cpp:12:2: error: use of undeclared identifier 'filler_linker'
filler_linker = head_pointer;
^
main.cpp:12:18: error: use of undeclared identifier 'head_pointer'
filler_linker = head_pointer;
^
main.cpp:14:2: error: use of undeclared identifier 'filler_link'
filler_link -> next = new link(NULL, *new_string);
^
main.cpp:14:39: error: indirection requires pointer operand ('std::string' (aka
'basic_string<char>') invalid)
filler_link -> next = new link(NULL, *new_string);
^~~~~~~~~~~
main.cpp:15:2: error: use of undeclared identifier 'head_ponter'
head_ponter = NULL;
^
main.cpp:17:1: error: use of undeclared identifier 'filler_link'
filler_link = filler_linker -> next;
^
main.cpp:17:15: error: use of undeclared identifier 'filler_linker'
filler_link = filler_linker -> next;
^
main.cpp:19:1: error: use of undeclared identifier 'head_pointer'
head_pointer -> printAll(head_pointer);
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
link.cpp:2:1: error: use of undeclared identifier 'link'
link::link(link * pointer_to_link, string * pointer_to_string)
^
link.cpp:2:36: error: unknown type name 'string'; did you mean 'std::string'?
link::link(link * pointer_to_link, string * pointer_to_string)
^~~~~~
std::string
/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/bits/stringfwd.h:62:33: note:
'std::string' declared here
typedef basic_string<char> string;
^
link.cpp:2:12: error: unknown type name 'link'
link::link(link * pointer_to_link, string * pointer_to_string)
^
link.cpp:7:1: error: use of undeclared identifier 'link'
link::~link() {
^
link.cpp:7:8: error: expected the class name after '~' to name a destructor
link::~link() {
^
link.cpp:10:1: error: unknown type name 'link'
link * link::getNext() {
^
link.cpp:10:8: error: use of undeclared identifier 'link'
link * link::getNext() {
^
link.cpp:13:15: error: use of undeclared identifier 'link'
std::string * link::getString() {
^
link.cpp:16:5: error: use of undeclared identifier 'link'
int link::printAll(link link_to_print) {
^
link.cpp:16:20: error: unknown type name 'link'
int link::printAll(link link_to_print) {
^
10 errors generated.