尝试在C++中使用列表实现class的方法时出现以下错误!

Having the following error when trying to implement a method of class using list in C++!

编辑 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;
}

使用

vehicule->addData(VIN, miles, dealer, price);

而不是

vehicule.->addData(VIN, miles, dealer, price);

您已将 vehicule 声明为指针,但您正试图访问带有“.”的方法。操作员。使用指针时需要使用“->”。比如:vehicule->addData(VIN, miles, dealer, price);

您需要更新任何引用车辆的代码。

好的,我看到了这两个问题。您已将 vehicule 声明为指针并分配了一些内存。

Cars *vehicule = new Cars();

然后你这样调用 vehicule:

vehicule.->addData(VIN, miles, dealer, price);
vehicule.addToBase(outfile);

击打以上

电话应该是:

vehicule->addData(VIN, miles, dealer, price);
vehicule->addToBase(outfile);

虽然这看起来只是一小段代码,但始终建议您在完成分配后释放内存。

由于您在列表中添加vehiculecarsList,工作完成后,请清理列表。

你可以这样做,像这样:

carsList.clear();

希望这对您有所帮助,听起来您是新手。所以,尽可能详细。