我最近在看C++ Primer Plus,做编程题遇到的问题,五章第6题
题目内容大概:
构造一个car体,里面有生产商,生产年份.请用户输入,然后输出.
我写的代码:好象指针的用法没掌握好,还有动态分配内存的问题.
#include "stdafx.h "
#include <iostream>
#include <string>
using namespace std;
struct car
{
string maker; //汽车生产商
int year; //汽车生产的年份
};
int main()
{
int n=0;
int i=0;
cout < < "How mang cars do you wish to catalog? ";
cin> > n;
car* p=new car[n];
for(i;i <n;i++){
cout < < "car # " < <i+1 < < ":\n ";
cout < < "please enter the maker: ";
cin> > p[i]-> maker;
cout < < "please enter the year: ";
cin> > p[i]-> year;
}
cout < < "Here is your collection:\n ";
for(i;i <n;i++){
cout < <p[i]-> year < < " " < <p[i]-> maker < <endl;
}
return 0;
}
-------------------Configuration: 5_6 - Win32 Debug--------------------
Compiling...
5_6.cpp
F:\C++ primer编程练习\5_6\5_6.cpp(26) : error C2819: type 'car ' does not have an overloaded member 'operator -> '
F:\C++ primer编程练习\5_6\5_6.cpp(11) : see declaration of 'car '
F:\C++ primer编程练习\5_6\5_6.cpp(26) : error C2227: left of '-> maker ' must point to class/struct/union
F:\C++ primer编程练习\5_6\5_6.cpp(28) : error C2819: type 'car ' does not have an overloaded member 'operator -> '
F:\C++ primer编程练习\5_6\5_6.cpp(11) : see declaration of 'car '
F:\C++ primer编程练习\5_6\5_6.cpp(28) : error C2227: left of '-> year ' must point to class/struct/union
F:\C++ primer编程练习\5_6\5_6.cpp(33) : error C2819: type 'car ' does not have an overloaded member 'operator -> '
F:\C++ primer编程练习\5_6\5_6.cpp(11) : see declaration of 'car '
F:\C++ primer编程练习\5_6\5_6.cpp(33) : error C2227: left of '-> year ' must point to class/struct/union
F:\C++ primer编程练习\5_6\5_6.cpp(33) : error C2819: type 'car ' does not have an overloaded member 'operator -> '
F:\C++ primer编程练习\5_6\5_6.cpp(11) : see declaration of 'car '
F:\C++ primer编程练习\5_6\5_6.cpp(33) : error C2227: left of '-> maker ' must point to class/struct/union
Error executing cl.exe.
5_6.exe - 8 error(s), 0 warning(s)
请帮忙解决一下.谢谢,希望最好能解释一下.
[解决办法]
#include <iostream>
#include <string>
using namespace std;
struct car
{
string maker; //汽车生产商
int year; //汽车生产的年份
};
int main()
{
int n=0;
int i=0;
cout < < "How mang cars do you wish to catalog? ";
cin> > n;
car* p = new car[n];
for(i;i <n;i++){
cout < < "car # " < <i+1 < < ":\n ";
cout < < "please enter the maker: ";
cin> > p[i].maker;
cout < < "please enter the year: ";
cin> > p[i].year;
}
cout < < "Here is your collection:\n ";
for(i;i <n;i++){
cout < <p[i].year < < " " < <p[i].maker < <endl;
}
return 0;
}
[解决办法]
struct car
{
string maker;
int year;
};
int main()
{
int n = 0;
int i = 0;
cout < < "How mang cars do you wish to catalog? ";
cin > > n;
car* p = new car[n];
for(;i < n; i++){
cout < < "car # " < < i+1 < < ":\n ";
cout < < "please enter the maker: ";
cin > > p[i].maker; // 对象地址访问才用->
cout < < "please enter the year: ";
cin > > p[i].year;
}
cout < < "Here is your collection:\n ";
for(i = 0 ;i <n;i++) //i重新赋值为0
{
cout < <p[i].year < < " " < <p[i].maker < <endl;
}
return 0;
}
[解决办法]
#include <iostream>
#include <string>
using namespace std;
typedef struct car
{
string maker; //汽车生产商
int year; //汽车生产的年份
}CAR;
int main()
{
int n=0;
int i=0;
cout < < "How mang cars do you wish to catalog? ";
cin> > n;
CAR *p=new CAR[n];
for(i;i <n;i++){
cout < < "car # " < <i+1 < < ":\n ";
cout < < "please enter the maker: ";
cin> > p[i].maker;
cout < < "please enter the year: ";
cin> > p[i].year;
}
cout < < "Here is your collection:\n ";
for(i=0;i <n;i++){
cout < <p[i].year < < " " < <p[i].maker < <endl;
}
return 0;
}