嗨,大家好,我昨天开始学习C++。't'未在此范围内声明
我想创建依赖于我从文件中读取的值的对象。然而它是说价值t没有在范围内声明:
另外我明白,如果我做我的代码的方式不一定是最佳实践。作为一般的编码概念,我不知道我怎么会初始化牛逼事先为我创建的对象取决于给予
这里的价值是代码:
while(getline(linestream,value,',')){
if(i==0){
cout<< "Type " << value << endl;
type = value;
}
else if(i==1){
cout<< "Code " << value << endl;
code = value;
}
else if (i==2){
cout << "Count " << value << endl;
count = atoi(value.c_str());
}
else if (i ==3){
cout << "Price " << value << endl;
price = atoi(value.c_str());
}
else if(i ==4){
cout << "Other " << value << endl;
other = value;
}
i++;
if(i ==5){
if(type == "transistor"){
Transistor *t = new Transistor(code,count,price,other);
}else if (type == "IC"){
IC *t = new IC(code,count,price,other);
}else if (type == "resistor"){
Resistor *t = new Resistor(code,count,price,other);
}else if (type == "capacitor"){
Capacitor *t = new Capacitor(code,count,price,other);
}else{
Diode *t = new Diode(code,count,price,other);
}
if(counter ==0){
LinkedList list(t);
}else{
list.tailAppend(t);
}
}
}
而且所有的课,我创建潜在的对象都是从基类派生的股票项目
是'Transistor','Resistor'等某些类的所有子类(例如'Component')吗?如果是这样,那么你可以在if检查之前声明一个'Component * t',然后根据执行的'if'语句使用多态来初始化它。 – ArchbishopOfBanterbury
是@ArchbishopOfBanterbury(爱这个名字btw)他们都是从一个基类派生出来的,但是它的抽象基类 – Blarsssss
@Brasssss没关系,你仍然可以创建一个指向这个基类的指针,然后为它指派一个派生的实例如你在'if'检查中所做的那样 - 例如:首先声明'Base * t',然后对每个if语句执行't = new Transistor(...)'等等。 – ArchbishopOfBanterbury