标题:请教大家一个问题
只看楼主
nwpu063417
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:428
专家分:28
注 册:2007-5-11
结帖率:60%
 问题点数:0 回复次数:4 
请教大家一个问题
初学c++设计一个通讯录,其中有一个Person类,充当链表结点,还有一个主函数。
   有报错:
--------------------Configuration: PhoneNumberList - Win32 Debug--------------------
Compiling...
test.cpp
c:\documents and settings\administrator\桌面\phonenumberlist\person.h(29) : error C2572: 'Person::Person' : redefinition of default parameter : parameter 3
        c:\documents and settings\administrator\桌面\phonenumberlist\person.h(24) : see declaration of 'Person::Person'
c:\documents and settings\administrator\桌面\phonenumberlist\person.h(30) : error C2440: '=' : cannot convert from 'const char *' to 'char [20]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
c:\documents and settings\administrator\桌面\phonenumberlist\person.h(31) : error C2440: '=' : cannot convert from 'const char *' to 'char [20]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
Error executing cl.exe.

test.obj - 3 error(s), 0 warning(s)
搜索更多相关主题的帖子: Person documents parameter person settings 
2008-02-13 09:00
nwpu063417
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:428
专家分:28
注 册:2007-5-11
得分:0 
//Person.h
#include <stdlib.h>

class Person
{
public:
    char name[20];
    char num[20];
    Person *next;

    Person(Person *ptrNext = NULL)
    {
        next = ptrNext;
    }
    Person(const char *initName, const char *initNum, Person *ptrNext = NULL);
    ~Person(){}
};

Person::Person(const char *initName, const char *initNum, Person *ptrNext = NULL)
{
    name = initName;
    num = initNum;
    next = ptrNext;
}

//test.cpp
#include "Person.h"
#include <iostream.h>

int main()
{
    Person p;
    Person p1("wang", "12345");
    Person p2("li", "34567");

    p.next = &p1;
    p1.next = &p2;

    cout<<p1.next->name<<endl;

    return 0;
}

2008-02-13 09:02
nwpu063417
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:428
专家分:28
注 册:2007-5-11
得分:0 
Person(const char *initName, const char *initNum, Person *ptrNext = NULL);
首先  Person *ptrNext = NULL  怎么是重定义呢?

然后 用一个字符指针作参数给一个字符数组赋值,不可以么?

2008-02-15 09:36
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
#include <stdlib.h>
#include <iostream.h>
class Person
{
public:
    char *name;
    char *num;
    Person *next;


    Person()
    {   Person *ptrNext = NULL;
        next = ptrNext;
    }
Person::Person( char *initName,  char *initNum, Person *ptrNext = NULL)
{
    name = initName;
    num = initNum;
    next = ptrNext;
}
    ~Person(){}
};


int main()
{
    Person p;
    Person p1("wang", "12345");
    Person p2("li", "34567");

    p.next = &p1;
    p1.next = &p2;

    cout<<p1.next->name<<endl;

    return 0;
}
2008-02-15 20:08
nwpu063417
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:428
专家分:28
注 册:2007-5-11
得分:0 
谢谢楼上这位兄弟,能具体解释一下么?
为什么不能用一个字符指针作参数给一个字符数组赋值?
谢谢

2008-02-15 21:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-199767-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014972 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved