【发布时间】:2015-03-26 21:21:24
【问题描述】:
好的,我不知道如何解释,但这里是。我想将 Dog 和 Cat 类的名称(从返回名称)获取到 int main 中,以便它们打印出 fido.name 和 spot.name 的位置。我该怎么做?
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Dog {
private:
// constructor
Dog(string name) {
this->name = name;
name = "Fido";
cout << "Dog's name is " << name << endl;
}
public:
static string name;
static string GetName();
};
string Dog::GetName(){
return name;
}
class Cat {
private :
// constructor
Cat(string name) {
this->name = name;
name = "Fido";
cout << "Cat's name is " << name << endl;
}
public :
static string name;
static string GetName();
};
string Cat::GetName(){
return name;
}
int main() {
Dog fido("Fido"); //error here stating that Dog::Dog(std::string name)
//declared at line 13 is inaccessible
Cat spot("Spot");
cout << "From main, the Dog's name is " << fido.name << endl;
cout << "From main, the Cat's name is " << spot.name << endl;
cout << "Hit any key to continue" << endl;
system("pause");
return 0;
}
【问题讨论】:
-
您可能想阅读The Definitive C++ Book Guide and List,因为您遇到的问题非常基本。