spa*_*ark 2 c++ coding-style visual-studio-2010
我刚刚从C#切换到C++我在C++中编写了一个链接列表代码在win32控制台应用程序中运行它并在构建时遇到非常奇怪的错误
我在评论中指出了3个错误,其余的我不能打字,它太多了.
using namespace std;
class float_list
{
struct node
{
double data;
struct node *next;
};
node *head;
public:
float_list(void)
{
head = nullptr;
};
void appendNode(double);
};
//void float_list::appendNode(float num)
//{
//
//}
void float_list::appendNode(double num)
{
node *newNode;
node *ptr; //here i am getting this Error error C3872:
//'0xa0': this character is not allowed in an identifier ,
// how ever I changed its name again and again.
newNode = new node;
newNode->data = num; // here un declared identifier ,
//also missing ; before this line
newNode->next = nullptr;
if (!head)
{
head = newNode;
}
else
{
ptr = head;
while (ptr->next)
{
ptr = ptr->next;
ptr->next = newNode;
};
}
}
Run Code Online (Sandbox Code Playgroud)
问题可能不是标识符,而是它周围的空白区域. 0xA0是一个非破坏空间的Latin-1代码.它不是输入中的合法字符,并且由于某种原因,编译器将其视为标识符的一部分.如果没有其他工作,删除该行并重新输入,确保所有空格都是正常空格.(我不确定在Windows下,但我认为控制空间或移位空间将进入不间断的空间.)
其他人帮助你以某种方式粘贴到你的代码中的无效字符; 但顺便说一句,我认为你的上一个while循环有一个错误:ptr->next = newNode;应该在循环之外:
void float_list::appendNode(double num)
{
// ...
if (!head)
{
head = newNode;
}
else
{
ptr = head;
while (ptr->next)
{
ptr = ptr->next;
// ptr->next = newNode;
};
ptr->next = newNode; // here - at the end of the list
}
}
Run Code Online (Sandbox Code Playgroud)
此外,最好为列表维护头节点和最后一个节点指针; 这样你就不需要在每次新的调用中反复遍历整个列表appendNode.
| 归档时间: |
|
| 查看次数: |
5392 次 |
| 最近记录: |