int main()
{
string name, sound, owner;
int age;
int answer = 1;
int i = 0;
do
{
++i;
puts("Enter the dog info below");
puts("Dog's name: ");
cin >> name;
puts("Dog's sound: ");
cin >> sound;
puts("Dog's age: ");
cin >> age;
puts("Dog's owner: ");
cin >> owner;
puts("Do you want to add one more dogs to the database?\n1: Yes\n0: No");
cin >> answer;
Dog name(name, sound, age, owner);
} while (answer != 0);
for (int a = i; i > 0; i--)
{
printf("%s", name.getname().c_str());
printf("\n\n%s is a dog who is %d years old, says %s and %s the owner\n\n",
name.getname().c_str(), name.getage(), name.getsound().c_str(), name.getowner().c_str());
}
return 0;
}
這是用於根據用戶輸入創建多個對象的簡單代碼。我設置了類和方法。沒有do while循環,它工作得很好。但我不能根據用戶輸入創建對象並打印它們。以下行顯示錯誤「沒有成員getname」。每個調用的方法都有相同的錯誤。我明白爲什麼會發生這種情況,但有沒有解決方案?如何根據用戶輸入創建和訪問多個對象 - C++
name.getname().c_str(), name.getage(), name.getsound().c_str(), name.getowner().c_str());
向我們展示你的'Dog'類。 – user1286901
「我明白爲什麼會發生這種情況」 - 您是否介意解釋爲什麼會發生這種情況以幫助衡量您的知識水平? – immibis