【发布时间】:2013-12-09 02:25:18
【问题描述】:
我试图在运行时创建对象。我有未指定数量的对象要创建,我完全迷失了。有人告诉我要使用指针来执行此操作,但即使使用指针我也不知道该去哪里。
这是我的代码。
#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;
}
【问题讨论】:
-
提示:你构造的签名不匹配
-
谢谢,都是玩的,还没清理测试部分。
-
这是你的真实代码吗?它不应该编译..
-
它没有。我在编程方面很新。这些是我试图开始工作的测试项目。它不编译。在提交之前,我没有解决所有测试错误。对不起。