【问题标题】:what is mistakes/errors in this code c++ tell me the correction?这段代码中的错误/错误是什么 c++ 告诉我更正?
【发布时间】:2010-03-16 20:06:13
【问题描述】:

在这段代码中编译器打印错误: 132 C:.... `createlist' 未声明(首先使用此函数) (每个未声明的标识符只针对它出现的每个函数报告一次。)

并在 main 函数的所有调用中再次重复它:( 有什么问题 ??请帮帮我

#include<iostream>
#include<string>
using namespace std;

template <typename T>
struct Node
{

T num;
struct Node<T> *next;

// to craet list nodes
void createlist(Node<T> *p)
{ T data;
   for(    ;    ;    ) // its containue until user want to stop
    { cout<<"enter data number or '#' to stop\n";
     cin>>data;
      if(data == '#')
       { p->next =NULL;
                  break;
       } 
         else
                { p->num= data;
                  p->next = new Node<T>;
                  p=p->next;
                }
    }
}



//count list to use it in sort function
int countlist (Node<T> *p)
{
int count=0;
while(p->next != NULL)
    {  count++;
        p=p->next;
    }
     return count;
}

// sort list
void sort( Node<T> *p)
{ Node<T> *p1, *p2; //element 1 & 2 to compare between them
 int i, j , n;
 T temp;

n= countlist(p);
for( i=1; i<n ; i++)
{ // here every loop time we put the first element in list in p1 and the second in p2
p1=p;
p2=p->next;
   for(j=1; j<=(n-i) ; j++)
   {
     if( p1->num > p2->num)
     { temp=p2->num;
      p2->num=p1->num;
      p1->num=temp;
      } 
    }
   p1= p1->next;
   p2= p2->next;
}
}


//add  new number in any location the user choose
void insertatloc(Node<T> *p)
{ T n; //read new num
   int loc; //read the choosen location
   Node<T> *locadd, *newnum, *temp;

  cout <<" enter location you want ..! \n";
  cin>>loc;
  locadd=NULL; //make it null to checked if there is location after read it from user ot not
  while(p->next !=NULL)
  { if( p->next==loc)
        { locadd=p;
                break;
        }
     p=p->next;
  }

if (locadd==NULL)
{cout<<" cannot find the location\n";}
else //if location is right 
{cout<<" enter new number\n"; // new number to creat also new location for it
  cin>>n;
  newnum= new Node/*<T>*/;
  newnum->num=n;
  temp= locadd->next;
  locadd->next=newnum;
  newnum->next=temp;

 }
locadd->num=sort(locadd); // call sort function 
}




// display all list nodes

void displaylist (Node<T> *p)
{
while (p->next != NULL)
      {
        cout<<" the list contain:\n";
        cout<<p->num<<endl;
        p=p->next;
      }
}

};//end streuct

int main()
{

cout<<"*** Welcome  in Linked List Sheet 2****\n";

// defined pointer for structer Node
// that value is the address of first node


struct Node<int>*mynodes= new struct Node<int>;



// create nodes in mynodes list
cout<<"\nCreate nodes in list";
createlist(mynodes);


// insert node in location
insertatloc(mynodes);



/* count the number of all nodes
nodescount = countlist(mynodes);
cout<<"\nThe number of nodes in list is: "<<nodescount;*/



// sort nodes in list
sort(mynodes);



// Display nodes
cout<<"\nDisplay all nodes in list:\n";
displaylist(mynodes);


system("pause");
return 0;
}

【问题讨论】:

  • @jeje 欢迎来到 Stack Overflow。为获得最佳结果,请在将来格式化您的代码。您可以访问stackoverflow.com/posts/2457704/edit 查看我对您的帖子所做的更改以实现正确的格式
  • 以后也请缩进你的代码。不疼。
  • 这给了你多少?您的成员函数不需要将Node 作为参数,因为从概念上讲,该函数已经是实例的一部分;直接对成员进行操作。

标签: c++


【解决方案1】:

createlist 是 Node 类的方法,但在 main() 内部,您将其作为函数调用。我建议将 Node 视为 C-struct 并将这些方法实现为采用 struct 的函数,就像 Thomas 提到的那样,这就是您的代码的结构方式,或者 reading a tutorial on C++ classes。

【讨论】:

    【解决方案2】:

    我的猜测是您的节点结构缺少一个结束“}”:

    template <typename T>
    struct Node
    {
    
    T num;
    struct Node<T> *next;
    
    }; // <--- add this line.
    

    【讨论】:

      【解决方案3】:

      createlist 被定义为采用Node&lt;T&gt; *p 的参数,但您传递给它的是struct Node&lt;int&gt;* 的实例

      【讨论】:

        【解决方案4】:

        createlist 是 Node.js 的成员方法。您正试图从 main 访问 Node::createlist。您不能这样做(即使您在调用中添加“Node::”范围),因为 createlist 不是静态方法。

        改成:

        // to create list nodes
        static void createlist(Node<T> *p)
        

        和:

        // create nodes in mynodes list
        cout<<"\nCreate nodes in list";
        Node::createlist(mynodes);
        

        或者将 createlist 完全从 Node 类中提取出来,并使其成为自己的函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-07
          • 2012-12-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多