【发布时间】:2015-09-11 21:29:25
【问题描述】:
编辑 1
初步构想:
MAIN.CPP:
#include <cstdlib>
#include "Cars.h"
#include "Dealer.h"
#include "Manufacturer.h"
#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
#include <iomanip>
#include <list>
using namespace std;
//Instance variables for each class
string VIN = " ";
int miles;
string dealer = " ";
int price;
string vinCode=" ";
string manuCode = " ";
string manuName = " ";
string dealerName = " ";
int zipcode;
string dealerPhone = " ";
int main(int argc, char** argv) {
char command;
//Cars vehicule;
Manufacturer maker;
Dealer dealership;
ifstream infile;
ofstream outfile;
list <Cars> carsList;
//Checks if the data file exists
infile.open("database.txt", ifstream::in);
outfile.open("database.txt", ios_base::app);
//each command is a different program option
cout << "Enter a command:" << endl;
cin >> command;
while (command!='q')
{
switch (command)
{
case 'a':
{
cin >> command;
//adds a car
if (command=='c')
{
//creates a new car object and calls constructor
Cars *vehicule = new Cars();
//gets user input a assign then to variables
//for the method calls
cin >> VIN >> miles >> dealer >> price;
// 1. this is were the compiler complains
vehicule.->addData(VIN, miles, dealer, price);
vehicule.addToBase(outfile);
carsList.push_back(vehicule);
list<Cars*>::iterator it;
for(it=carsList.begin(); it!=carsList.end(); it++)
{
cout << *it->getVIN() << endl; // compile error
}
}
break;
}
//new command to keep the while loop going
cout << "Enter a command:" << endl;
cin >> command;
}
}
outfile.close();
return 0;
}
CARS.H:
#ifndef CARS_H
#define CARS_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
//Object that contains all information about cars (this is the class documentation)
class Cars {
public:
//class methods
*Cars(){VIN=" "; mileage=0; dealership=" "; price=0;}
void addData(string, int, string, int);
void addToBase(ofstream&);
string getVin(){return this->VIN;}
int getMiles(){return this->mileage;}
string getDealer(){return this->dealership;}
int getPrice(){return this->price;}
//private variables containing object information
private:
string VIN;
int mileage;
string dealership;
int price;
string vinCode;
};
void Cars::addData(string identification, int mile, string dealer, int money)
{
VIN=identification;
mileage=mile;
dealership=dealer;
price=money;
vinCode = VIN.substr(0,3);
return;
}
void Cars::addToBase(ofstream& file)
{
file << "c" << endl << VIN << endl << mileage << endl <<
dealership << endl << price << endl;
return;
}
编辑 2
到目前为止我得到的新版本:
#include "Car.h"
#include "Dealer.h"
#include "Manufacturer.h"
#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
#include <iomanip>
#include <list>
using namespace std;
string VIN;
int miles;
string dealer;
int price;
string vinCode;
string manuCode;
string manuName;
string dealerName;
int zipcode;
string dealerPhone;
int main(int argc, char** argv) {
char command;
ifstream infile;
ofstream outfile;
list<Car*> carsList;
//Checks if the data file exists
infile.open("database.txt", ifstream::in);
outfile.open("database.txt", ios_base::app);
//Reads in user input
cout << "Enter a command:" << endl;
cin >> command;
while (command != 'q')
{
switch (command)
{
case 'a': //Add
{
cin >> command;
if (command == 'c') //Add car
{
cin >> VIN >> miles >> dealer >> price;
Car* vehicule = new Car(VIN, miles, dealer, price); //New pointer
vehicule->addToBase(outfile);
carsList.push_back(vehicule);
list<Car*>::const_iterator iterator;
for (std::list<Car*>::const_iterator iterator = carsList.begin(),
end = carsList.end(); iterator != end; ++iterator)
{
cout << (*iterator)->getVin();
}
//end of for loop
}//end of if loop
}//end of case loop
break;
}//end of switch loop
cout << "Enter a command:" << endl;
cin >> command;
}//end of while loop
infile.close();
outfile.close();
return 0;
}
我仍然收到错误:
"/Applications/Xcode.app/Contents/Developer/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf "/Applications/Xcode.app/Contents/Developer/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/project_1 make[2]:
*** No rule to make target `newcppsimpletest.cpp', needed by `build/Debug/GNU-MacOSX/newcppsimpletest.o'. Stop. make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2
Car.h:
#ifndef CARS_H
#define CARS_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Car {
public:
Car();
Car(string, int, string, int);
void addToBase(ofstream&);
string getVin(){return this->VIN;}
int getMiles(){return this->mileage;}
string getDealer(){return this->dealership;}
int getPrice(){return this->price;}
string getVinCode(){return this->vinCode;}
private:
string VIN;
int mileage;
string dealership;
int price;
string vinCode;
};
Car::Car()
{
string VIN;
int mileage=0;
string dealership;
int price=0;
string vinCode;
}
Car::Car(string vin, int miles, string carDealer, int dollars)
{
string VIN=vin;
int mileage=miles;
string dealership=carDealer;
int price=dollars;
string vinCode = VIN.substr(0,3);
}
void Car::addToBase(ofstream& file)
{
file << "c" << endl << VIN << endl << mileage << endl <<
dealership << endl << price << endl;
return;
}
【问题讨论】:
-
line 77 main.cpp:74:30: error: expected unqualified-id-id vehicule.->addData(VIN, 英里, 经销商, 价格);
-
好的,应该说‘出了什么问题?’。
-
顺便说一句,我为错误的格式道歉,第一次在 Stack 上发帖会回复任何问题
-
编译器会建议
->,而不是.->(注意缺少.)。但是,我建议(如果我没记错的话,这里还有其他几个人)改为将Cars *vehicule = new Cars()更改为Cars vehicule;(即,使其不是指针) -
我没有详细阅读你的新代码,但是你得到的错误信息是由于你使用的IDE造成的。不知何故,文件
newcppsimpletest.cpp没有正确添加到您的项目中,编译器不知道它在哪里。不幸的是,如果没有更多信息,我无法提供太多帮助。
标签: c++ database string iomanip