C++ 创建带有节点C++;
我正在使用3个文件: hw10.hC++ 创建带有节点C++;,c++,struct,linked-list,header-files,nodes,C++,Struct,Linked List,Header Files,Nodes,我正在使用3个文件: hw10.h #ifndef Structures_hw10 #define Structures_hw10 #include <iostream> struct Node{ int value; Node* next; }; struct LinkedList{ Node* head = NULL; }; void append(int); #endif main.cpp #include "hw10.h" void LinkedLi
#ifndef Structures_hw10
#define Structures_hw10
#include <iostream>
struct Node{
int value;
Node* next;
};
struct LinkedList{
Node* head = NULL;
};
void append(int);
#endif
main.cpp
#include "hw10.h"
void LinkedList::append(int data){
Node* cur = head;
Node* tmp = new Node;
tmp->value = data;
tmp->next = NULL;
if(cur->next == NULL) {
head = tmp;
}
else {
while(cur->next != NULL){
cur = cur->next;
}
cur->next = tmp;
}
// delete cur;
}
#include "hw10.h"
int main(){
LinkedList LL;
LL.append(5);
LL.append(6);
Node* cur = LL.head;
while(cur->next != NULL){
std::cout<<cur->value<<std::endl;
cur = cur->next;
}
return 0;
}
这是我收到的答复:
In file included from main.cpp:2:0:
hw10.h:13:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
In file included from hw10.cpp:1:0:
hw10.h:13:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
hw10.cpp: In function 'void append(int)':
hw10.cpp:10:15: error: 'head' was not declared in this scope
我的主要功能是创建一个新的链表,附加两个新节点,并打印出它们的值(以确保它正常工作) 在您的结构声明中,您必须像这样在结构中添加append
struct LinkedList{
Node* head = NULL;
void append(int);
};
尝试添加“-std=c++11”以消除警告。我相信c++11添加了初始化结构中成员的功能。错误指出在hw10.cpp的第10行使用head,但head在第9行而不是第10行,看起来还可以。这看起来不像是编译的源代码,是吗?@koodawg这是我写的源代码。我编译了你的代码,得到了一个完全不同的错误,请看下面我的答案。
struct LinkedList{
Node* head = NULL;
void append(int);
};