2

名前である構造体の1つで関数をテストしただけですが、それらに到達しません。これはこれまでの完全なコードです:

アップデート:

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

struct Node{
    string name;
    string address;
    int phoneNum;
    struct Node* next;
};

Node insertInOrder(Node p, string theName);
void push(struct Node*& head, string theName);


int main(){

    cout<<"\tEnter S or s to show the list contents \n"
        <<"\tEnter A or a to add a node to the list \n"
        <<"\tEnter D or d to delete a node from the list \n"
        <<"\tEnter Q or q to quiet the program \n"
    <<"Make your selection: "<<endl;

    struct Node* newNode = new Node;
    push(newNode, "yeah");

    cout<<newNode;

    return 0;

}

void push(struct Node*& head, string theName){
    struct Node* newNode = new Node;
    newNode->name = theName;
    newNode->next = head;
    head = newNode;
}

Node insertInOrder(Node p, string theName){
    if(p == NULL || p.name >= theName){
        return new Node(p, theName);
    }
    else{
            p.next = insertInOrder(p.next, theName);
            return p;
        }
    }

次のようなエラーが発生します:このコードの不完全なタイプ「Node」への「sizeof」の無効な適用:

    void push(struct Node*& head, string theName){
    struct Node* newNode = malloc(sizeof(struct Node));
    newNode->name = theName;
    newNode->next = head;
    head = newNode;
}

このコードを自分のコードに変換しようとしていますが、エラーが発生しました:

 Node insertInOrder( int k, Node p ) {
   if( p == " " || p.item >= k ) 
      return new Node( k, p ); 
   else {
      p.next = insertInOrder( k, p.next ); 
      return p;
   }
} 

これが私がそれを翻訳した方法です:

Node insertInOrder(Node p, string theName){
    if(p == " " || p.name >= theName){
        return new Node(p, theName);
    }
    else{
            p.next = insertInOrder(p.next, theName);
            return p;
        }
    }

このコードのエラーは次のとおりです。

 if(p == " " || p.name >= theName){
        return new Node(p, theName);

エラー:

- comparison with string literal results in unspecified behaviour [-Waddress]
- request for member ‘name’ in ‘p’, which is of pointer type ‘Node*’ (maybe you meant to use ‘-
 >’ ?)
- comparison between distinct pointer types ‘Node*’ and ‘const char*’ lacks a cast [-
 fpermissive]

p.next = insertInOrder(p.next、theName); pを返します。

エラー:

Invalid arguments ' Candidates are: Node insertInOrder(Node, std::basic_string<char,std::char_traits<char>,std::allocator<char>>) '
- could not convert ‘p.Node::next’ from ‘Node*’ to ‘Node’
4

1 に答える 1

1

いくつかのポイント:

  • mallocあなたはC++で作業しているので忘れて、とを使用newしてくださいdelete
  • ノードを使用するたびに構造体であることを再度指定する必要はありません。それでsizeof(Node)十分ですが、直接 malloc を使用することはありません。
  • あなたの関数Node insertInOrder(Node p, string theName)は具体的なノードを受け入れ、具体的なものを返しNodeますが、構造体内の次のフィールドは へのポインターNodeです。
  • 値と文字列リテラル ( ) の間で比較演算子を直接使用することはできません。フィールドのみp == " "をチェックする必要がありますname
于 2012-11-22T15:19:13.887 に答える