我想在運行時創建對象。我有一個未指定數量的對象來創建,我完全失去了。我被告知要使用指針來做這件事,但我不確定哪怕要使用指針。在C++運行時動態創建類對象
這是我的代碼。
#include <iostream>
#include <string>
using namespace std;
class Consumer
{
public:
int idNum;
string info;
int maxHours;
Consumer(int, string, int);
void display();
void newCons();
};
Consumer::Consumer()
{
idNum = id;
info = in ;
maxHours = hrs;
}
void Consumer::newCons()
{
int idn;
string npa;
int mhrs;
cout << "Please input the consumer's ID number." << endl;
cin >> idn;
cout << "Please input the consumer's full name, phone number, and address." << endl;
cout << "Do not press enter until you have entered all three." << endl;
cin >> npa;
cout << "Please input the max number of hours the consumer spends purchasing products each week." << endl;
cin >> mhrs;
Consumer anotherCons(idn, npa, mhrs);
return;
}
void Consumer::display()
{
return;
}
int main()
{
int i, howMany;
Consumer* anotherCons;
anotherCons = new Consumer();
anotherCons->newCons();
return 0;
}
提示:您構建的簽名不匹配 – yuan
謝謝,這所有的周圍打的,還沒有清理的測試部分了。 – jwhite
這是你的真實密碼嗎?它不應該編譯.. – 0x499602D2