code behaviour is strange

和自甴很熟 提交于 2019-12-14 02:11:38

问题


I just switched to C++ from C# I wrote a link list code in C++ ran it in win32 console application and getting very strange errors while build

I pointed out 3 errors in comments , rest I cant type ,its too much .

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;
                };
        }
    }

回答1:


The problem probably isn't the identifier, but the white space around it. 0xA0 is the Latin-1 code for a non-breaking space. It's not a legal character in input, and for some reason, the compiler is treating it as part of the identifier. If nothing else works, delete the line and reenter it, making sure that all spaces are normal spaces. (I'm not sure under Windows, but I think a control-space or a shift-space will enter the nonbreaking space.)




回答2:


Others helped you with the invalid character that got pasted into your code somehow; but BTW, I think there's an error in your last while loop: ptr->next = newNode; should be outside the loop:

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
  }
}

Also, it's better to maintain both head and last node pointers for a list; that way you won't need to traverse the whole list again and again on each new call to appendNode.




回答3:


The error complaining about the 0xa0 character, and the following semi-colon error are, I believe, both caused by a character that got accidentally copied into your code, which is a unicode character that you can't see, but just because you can't see it doesn't mean it's not there and wreaking havoc!




回答4:


I had the same problem with the 0xA0 non breaking space character. I resolved it by highlighting a single character, copying it using ^C, then doing a Find and Replace All with a normal space. You'll need to paste the non breaking space into the Find field using ^V.



来源:https://stackoverflow.com/questions/9436134/code-behaviour-is-strange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!