Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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++代码,使用MS VisualStudio 15理解类继承。构建并运行代码后,我收到一条消息,说MS VS已停止工作。如果有人能帮我理解我做错了什么,我将不胜感激 #include<cstdio> #include<string> #include<conio.h> using namespace std; // BASE CLASS class Animal { private: string _name; string _type; string _sound; Animal() {}; protected: Animal(const string &n, const string &t, const string &s) :_name(n), _type(t), _sound(s) {}; public: void speak() const; }; void Animal::speak() const { printf("%s, the %s says %s.\n", _name, _type, _sound); } // DERIVED CLASSES class Dog :public Animal { private: int walked; public: Dog(const string &n) :Animal(n, "dog", "woof"), walked(0) {}; int walk() { return ++walked; } }; int main(int argc, char ** argv) { Dog d("Jimmy"); d.speak(); printf("The dog has been walked %d time(s) today.\n", d.walk()); return 0; _getch(); } #包括 #包括 #包括 使用名称空间std; //基类 类动物{ 私人: 字符串\u名称; 字符串类型; 弦乐; 动物{}; 受保护的: 动物(常量字符串和n,常量字符串和t,常量字符串和s):\u名称(n),\u类型(t),\u声音(s){}; 公众: void speak()常量; }; void Animal::speak()常量{ printf(“%s,%s表示%s.\n”,\u名称,\u类型,\u声音); } //派生类 狗类:公共动物{ 私人: 内走; 公众: 狗(const string&n):动物(n,“狗”,“汪”),走(0){}; int walk(){return++walk;} }; intmain(intargc,字符**argv){ 狗d(“吉米”); d、 说(); printf(“今天狗被遛了%d次。\n”,d.walk()); 返回0; _getch(); }_C++_Class_Inheritance - Fatal编程技术网

需要使用C++;类继承 我试图运行下面的C++代码,使用MS VisualStudio 15理解类继承。构建并运行代码后,我收到一条消息,说MS VS已停止工作。如果有人能帮我理解我做错了什么,我将不胜感激 #include<cstdio> #include<string> #include<conio.h> using namespace std; // BASE CLASS class Animal { private: string _name; string _type; string _sound; Animal() {}; protected: Animal(const string &n, const string &t, const string &s) :_name(n), _type(t), _sound(s) {}; public: void speak() const; }; void Animal::speak() const { printf("%s, the %s says %s.\n", _name, _type, _sound); } // DERIVED CLASSES class Dog :public Animal { private: int walked; public: Dog(const string &n) :Animal(n, "dog", "woof"), walked(0) {}; int walk() { return ++walked; } }; int main(int argc, char ** argv) { Dog d("Jimmy"); d.speak(); printf("The dog has been walked %d time(s) today.\n", d.walk()); return 0; _getch(); } #包括 #包括 #包括 使用名称空间std; //基类 类动物{ 私人: 字符串\u名称; 字符串类型; 弦乐; 动物{}; 受保护的: 动物(常量字符串和n,常量字符串和t,常量字符串和s):\u名称(n),\u类型(t),\u声音(s){}; 公众: void speak()常量; }; void Animal::speak()常量{ printf(“%s,%s表示%s.\n”,\u名称,\u类型,\u声音); } //派生类 狗类:公共动物{ 私人: 内走; 公众: 狗(const string&n):动物(n,“狗”,“汪”),走(0){}; int walk(){return++walk;} }; intmain(intargc,字符**argv){ 狗d(“吉米”); d、 说(); printf(“今天狗被遛了%d次。\n”,d.walk()); 返回0; _getch(); }

需要使用C++;类继承 我试图运行下面的C++代码,使用MS VisualStudio 15理解类继承。构建并运行代码后,我收到一条消息,说MS VS已停止工作。如果有人能帮我理解我做错了什么,我将不胜感激 #include<cstdio> #include<string> #include<conio.h> using namespace std; // BASE CLASS class Animal { private: string _name; string _type; string _sound; Animal() {}; protected: Animal(const string &n, const string &t, const string &s) :_name(n), _type(t), _sound(s) {}; public: void speak() const; }; void Animal::speak() const { printf("%s, the %s says %s.\n", _name, _type, _sound); } // DERIVED CLASSES class Dog :public Animal { private: int walked; public: Dog(const string &n) :Animal(n, "dog", "woof"), walked(0) {}; int walk() { return ++walked; } }; int main(int argc, char ** argv) { Dog d("Jimmy"); d.speak(); printf("The dog has been walked %d time(s) today.\n", d.walk()); return 0; _getch(); } #包括 #包括 #包括 使用名称空间std; //基类 类动物{ 私人: 字符串\u名称; 字符串类型; 弦乐; 动物{}; 受保护的: 动物(常量字符串和n,常量字符串和t,常量字符串和s):\u名称(n),\u类型(t),\u声音(s){}; 公众: void speak()常量; }; void Animal::speak()常量{ printf(“%s,%s表示%s.\n”,\u名称,\u类型,\u声音); } //派生类 狗类:公共动物{ 私人: 内走; 公众: 狗(const string&n):动物(n,“狗”,“汪”),走(0){}; int walk(){return++walk;} }; intmain(intargc,字符**argv){ 狗d(“吉米”); d、 说(); printf(“今天狗被遛了%d次。\n”,d.walk()); 返回0; _getch(); },c++,class,inheritance,C++,Class,Inheritance,您不能以这种方式将std::string与printf()一起使用 使用 相反 < >我建议使用 STD::CUT以使所有的东西在C++中无缝工作。 您不能以这种方式将std::string与printf()一起使用 使用 相反 >我建议使用 STD::CUT,以使所有的东西都在C++中无缝工作。 < P>用 %s>代码>期望A,而不是代码STD::String ,它们不是同一件事。所以printf(“%s,%s表示%s.\n”,“名称,\u类型,\u声音)不起作用,它不应该编译 您可以使

您不能以这种方式将
std::string
printf()一起使用

使用

相反


< >我建议使用<代码> STD::CUT以使所有的东西在C++中无缝工作。 您不能以这种方式将
std::string
printf()一起使用

使用

相反


<> >我建议使用<代码> STD::CUT,以使所有的东西都在C++中无缝工作。

< P>用<代码> %s>代码>期望A,而不是<>代码STD::String ,它们不是同一件事。所以
printf(“%s,%s表示%s.\n”,“名称,\u类型,\u声音)不起作用,它不应该编译

您可以使用,它将返回一个
const char*
。比如

printf("%s, the %s says %s.\n", _name.c_str(), _type.c_str(), _sound.c_str());
或者使用
std::cout
std::string
类似:

cout << _name << ", the " << _type << " says " << _sound << ".\n";
cout使用
%s
需要一个字符串,而不是
std::string
,它们不是一回事。所以
printf(“%s,%s表示%s.\n”,“名称,\u类型,\u声音)不起作用,它不应该编译

您可以使用,它将返回一个
const char*
。比如

printf("%s, the %s says %s.\n", _name.c_str(), _type.c_str(), _sound.c_str());
或者使用
std::cout
std::string
类似:

cout << _name << ", the " << _type << " says " << _sound << ".\n";

cout问题在于speak方法试图使用printf打印字符串对象

这个。它在字符数组上工作,字符数组在C语言中用于表示字符串。 如果仍要使用printf,则需要将字符串转换为字符数组。这可以通过以下方式完成:

printf("%s, the %s says %s.\n", _name.c_str(), _type.c_str(), _sound.c_str());
更优雅的解决方案是使用std::cout以“C++方式”打印数据:

//include declaration at the top of the document
#include <iostream>
...
//outputs the result
cout <<_name + ", the " + _type + " says " + _sound << "." << endl;
//在文档顶部包含声明
#包括
...
//输出结果

cout问题在于speak方法试图使用printf来打印字符串对象

这个。它在字符数组上工作,字符数组在C语言中用于表示字符串。 如果仍要使用printf,则需要将字符串转换为字符数组。这可以通过以下方式完成:

printf("%s, the %s says %s.\n", _name.c_str(), _type.c_str(), _sound.c_str());
更优雅的解决方案是使用std::cout以“C++方式”打印数据:

//include declaration at the top of the document
#include <iostream>
...
//outputs the result
cout <<_name + ", the " + _type + " says " + _sound << "." << endl;
//在文档顶部包含声明
#包括
...
//输出结果

cout
有人可以帮我理解我做错了什么
你的VS2015中有钻石问题<代码>有人可以帮助我了解我做错了什么
您的VS2015Obey警告中有钻石问题!