Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++类成员初始化和构造函数定义 你好,我开始学习C++,现在我正在测试成员初始化器,我写了这个简单的代码: #include <iostream> #include <string> using namespace std; class Person { public: Person(); ~Person(); private: string p_name; string p_surname; int p_age; }; Person::Person(string name, string surname, int age) : p_name(name), p_surname(surname), p_age(age) { } Person::~Person() { } class MyClass { public: MyClass(int value) : m_value(value) { } private: int m_value; }; int main() { return 0; }_C++_C++11 - Fatal编程技术网

C++类成员初始化和构造函数定义 你好,我开始学习C++,现在我正在测试成员初始化器,我写了这个简单的代码: #include <iostream> #include <string> using namespace std; class Person { public: Person(); ~Person(); private: string p_name; string p_surname; int p_age; }; Person::Person(string name, string surname, int age) : p_name(name), p_surname(surname), p_age(age) { } Person::~Person() { } class MyClass { public: MyClass(int value) : m_value(value) { } private: int m_value; }; int main() { return 0; }

C++类成员初始化和构造函数定义 你好,我开始学习C++,现在我正在测试成员初始化器,我写了这个简单的代码: #include <iostream> #include <string> using namespace std; class Person { public: Person(); ~Person(); private: string p_name; string p_surname; int p_age; }; Person::Person(string name, string surname, int age) : p_name(name), p_surname(surname), p_age(age) { } Person::~Person() { } class MyClass { public: MyClass(int value) : m_value(value) { } private: int m_value; }; int main() { return 0; },c++,c++11,C++,C++11,然而,在Person类中,我得到了以下错误 错误1错误C2511:'Person::Personstd::string,std::string,int': 在中找不到重载的成员函数 “Person”c:\users\syd\documents\visualstudio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 19 1 consoleapplication1 第二节课也没有错误。如果

然而,在Person类中,我得到了以下错误

错误1错误C2511:'Person::Personstd::string,std::string,int': 在中找不到重载的成员函数 “Person”c:\users\syd\documents\visualstudio 2013\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 19 1 consoleapplication1


第二节课也没有错误。如果我没有弄错的话,我在Person类中以错误的方式声明构造函数,而解释器认为我重载了一个丢失的方法?我相信这样的错误对你们大多数人来说可能是愚蠢的,但如果有人能简单地解释我做错了什么,我将不胜感激

您需要将构造函数的原型(带有参数的原型)放在类Person中,而不是当前没有参数的原型

class Person
{
public:
    Person(string name, string surname, int age);
    ~Person();
private:

    string p_name;
    string p_surname;
    int p_age;

};
Person的声明与Personstring名称、string姓氏、int年龄的定义不匹配

在类声明中,将person更改为

public:
  Person(string name, string surname, int age);

您的原型还需要参数。个人字符串名称、字符串姓氏、整数年龄;您亲自声明的构造函数是默认的构造函数人员;但是您提供的定义包含3个参数,因此出现了错误。在类主体内,将构造函数声明更改为Personstring、string、int;你要找的词是编译器,而不是解释器:很抱歉,我已经习惯了web开发,所以我要找解释器。谢谢你的回答,不过这样就足够了吗?Personstring、string、int没有特定的变量,因为在VC中它是可以工作的,只是想知道这是一个坏习惯还是错误smthing@Syd是的,您可以拥有一个没有变量名的原型。语法是正确的。我认为这更多的是个人偏好——我也使用VC,我喜欢让intellisense在编写代码时告诉我给定函数的变量名称。当代码库非常大时,它会很有帮助。使用名称是很好的,因为它是一种说明类应该如何使用的文档形式。