C++ 摆脱;“未声明的标识符”;错误
所以我在做一个链表作业,在链表表格中给两个数字,把数字加起来,在链表表格中做最后的答案。我的代码不断出现“未声明标识符”错误,我想知道如何修复它 错误消息: List.cpp:48:3:错误:使用未声明的标识符“append” 附加(h,c) 谢谢C++ 摆脱;“未声明的标识符”;错误,c++,compiler-errors,linked-list,undeclared-identifier,C++,Compiler Errors,Linked List,Undeclared Identifier,所以我在做一个链表作业,在链表表格中给两个数字,把数字加起来,在链表表格中做最后的答案。我的代码不断出现“未声明标识符”错误,我想知道如何修复它 错误消息: List.cpp:48:3:错误:使用未声明的标识符“append” 附加(h,c) 谢谢 #include <iostream> #ifndef LISTNODE_H #define LISTNODE_H using namespace std; class ListNode { public: List
#include <iostream>
#ifndef LISTNODE_H
#define LISTNODE_H
using namespace std;
class ListNode {
public:
ListNode();
ListNode(int value, ListNode* next);
int value;
ListNode *next;
private:
friend class List;
};
#endif
#include <iostream>
#include "ListNode.h"
using namespace std;
ListNode:: ListNode() {
value = 'b';
next = NULL;
}
#include <iostream>
#include "ListNode.h"
#include <vector>
#ifndef LIST_H
#define LIST_H
using namespace std;
class List {
public:
List();
void append(ListNode* node, vector<char> c);
ListNode *head;
};
#endif
#include <iostream>
#include <string>
#include "List.h"
#include <vector>
#include "ListNode.h"
using namespace std;
void List:: append(ListNode *node, vector<char> c) {
//ListNode *temp
for(int i = 0; i < c.size(); i++) {
if(head == NULL) {
head = node;
}
else {
ListNode* itr = head;
while(itr -> next != NULL) {
itr = itr -> next;
}
node = itr -> next;
node -> value = c[i];
cout << node -> value << endl;
}
}
}
List:: List() { //Initializes the head and the tail for the whole class
head = NULL;
}
int main() {
ListNode *h;
string num1, num2, sentence;
vector<char> c;
cout << "Type in two numbers" << endl;
cout << "Number 1: " << endl;
cin >> num1;
cout << "Number 2: " << endl;
cin >> num2;
//cout << "Type in a sentence: " << endl;
//cin >> sentence;
cout << "--------" << endl;
for(int i = 0; i < num1.size(); i++) {
c.push_back(num1[i]);
}
append(h, c);
return 0;
}
#包括
#ifndef列表节点
#定义LISTNODE_H
使用名称空间std;
类ListNode{
公众:
ListNode();
ListNode(int值,ListNode*next);
int值;
ListNode*下一步;
私人:
好友类列表;
};
#恩迪夫
#包括
#包括“ListNode.h”
使用名称空间std;
ListNode::ListNode(){
值='b';
next=NULL;
}
#包括
#包括“ListNode.h”
#包括
#ifndef列表
#定义列表
使用名称空间std;
班级名单{
公众:
List();
void append(ListNode*节点,向量c);
ListNode*头;
};
#恩迪夫
#包括
#包括
#包括“List.h”
#包括
#包括“ListNode.h”
使用名称空间std;
void List::append(ListNode*节点,向量c){
//ListNode*temp
对于(int i=0;inext!=NULL){
itr=itr->next;
}
node=itr->next;
节点->值=c[i];
cout value此代码:
append(h, c);
调用名为append
的自由函数。但代码中没有此类函数
在List
类中确实有一个append
函数,但这是一个成员函数,因此需要一个List
对象来调用该函数。因此,需要类似以下内容:
List l;
l.append(h, c);
您在类列表中定义了成员函数append(顺便说一下,(函数)没有任何意义)
这与类列表无关
您甚至并没有在主目录中声明类型列表的对象
请注意,类ListNode具有类型为int
的数据成员
int value;
char value;
但是,您试图在此数据中保存char类型的成员对象,这些对象是声明为向量c
的向量的元素
而且完全不清楚为什么ListNode类的默认构造函数使用字符'b'
初始化数据成员值
ListNode:: ListNode() {
value = 'b';
next = NULL;
}
因此,整个代码没有意义
似乎您的意思是类ListNode的数据成员值的类型为char
,而不是int
int value;
char value;
在这种情况下,类List
的函数append
可以通过以下方式声明和定义
void List:: append( const std::vector<char> &v )
{
ListNode **current = &head;
while ( *current ) current = &( *current )->next;
for ( const auto &item : v )
{
*current = new ListNode( item, nullptr );
current = &( *current )->next;
}
}
void List::append(const std::vector&v)
{
列表节点**当前=&head;
而(*当前)当前=&(*当前)->下一步;
用于(常数自动和项目:v)
{
*当前=新的ListNode(项,nullptr);
当前=&(*当前)->下一步;
}
}
你需要一个<代码>列表>代码>对象,你在上面调用“代码>附加< /COD>”。方便阅读以理解这个答案中使用的一些术语:“未来工程123没有问题。如果解决了你的问题,考虑一下答案。我得到了我需要的。谢谢!”