C ++链表类错误

时间:2017-04-26 04:11:02

标签: 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;
}
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());
}
}

我有一个使用该类的main.cpp,如下所示:

的main.cpp

#include "link.h"
#include <iostream>
int main()
{
 for (int i = 0; i < 10; i++) {
 std::string new_string;
 std::getline(std::cin, new_string);
 link *  head_pointer = NULL;
 link *  filler_link = NULL;
 if (i == 0) {
 head_pointer -> next = new link(NULL, *new_string);
 filler_linker = head_pointer;
 } else {
 filler_link -> next = new link(NULL, *new_string);
 head_ponter = NULL;
}
filler_link = filler_linker -> next;
}
head_pointer -> printAll(head_pointer);
 return 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.

1 个答案:

答案 0 :(得分:1)

你需要在link.h中#include <string>。第一行告诉您编译器无法找到名为&#34; std&#34;的命名空间。在尝试声明std::string

此外,正如@Rogus在评论中指出的那样,你还需要在link.cpp中#include "link.h"。这导致错误发生在link.cpp中的错误列表的末尾。

相关问题