【问题标题】:Having the following error when trying to implement a method of class using list in C++!尝试使用 C++ 中的列表实现类的方法时出现以下错误!
【发布时间】: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 上发帖会回复任何问题
  • 编译器会建议-&gt;,而不是.-&gt;(注意缺少.)。但是,我建议(如果我没记错的话,这里还有其他几个人)改为将 Cars *vehicule = new Cars() 更改为 Cars vehicule;(即,使其不是指针)
  • 我没有详细阅读你的新代码,但是你得到的错误信息是由于你使用的IDE造成的。不知何故,文件newcppsimpletest.cpp 没有正确添加到您的项目中,编译器不知道它在哪里。不幸的是,如果没有更多信息,我无法提供太多帮助。

标签: c++ database string iomanip


【解决方案1】:

使用

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

代替

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

【讨论】:

  • 伙计,我觉得自己很愚蠢,我没有听懂。谢谢
【解决方案2】:

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

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

【讨论】:

  • 但是我有另一个类型的编译器错误:main.cpp:72:26: 错误:没有从 'Cars *' 到 'Cars' 的可行转换 Cars vehicule = new Cars(); @乔尔
  • @madeluccar new Cars() 返回一个 Cars* 并且您正试图将它分配给一个 Cars 变量。它是Cars* vehicule = new Cars();Cars vehicule;,取决于您是否需要指针。不完全推荐使用new,但如果你这样做了,你应该在某个地方有一个匹配的delete。我真的建议你读一本书什么的,你的代码有很多问题。
  • @Biffen 您还发现了哪些其他问题?实际上,当您使用 new 时,您应该创建一个 Cars 类型的新对象
  • @madeluccar 各种严重性的问题,我头顶上的问题:头文件中的实现代码(稍后会导致问题),奇怪的变量范围,“指针混淆”的大量案例,奇怪的类设计,缺少const,所有莫名其妙的" "字符串,内存泄漏。至于你的第二句话:我不明白你在说什么。
  • @Biffen 您说的是使用 new 创建了一个指针变量,但该行不应该创建一个 Cars 类型的新对象
【解决方案3】:

好的,我看到了这两个问题。您已将 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();

希望这会有所帮助,您听起来好像是新手。所以,尽量详细一点。

【讨论】:

  • carsList 会清除自己的内存,更让人担心的是没有deletenew
  • 您关于新建和删除的观点是正确的。但是,如果有重复的呼叫并且他正在使用 push_back,则清除变得相关。无论如何,对于基本代码,这些预防措施是没有意义的,因为程序会立即停止执行。
  • 您确实需要格式化答案中的代码以使其可读
  • 对不起,堆栈溢出有点新。千载难逢来这里一次。
  • @UmashankarDas 为什么提到删除列表中的指针? OP 没有任何指针列表。
猜你喜欢
  • 1970-01-01
  • 2020-09-12
  • 2015-12-10
  • 1970-01-01
  • 2020-07-31
  • 2013-06-16
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
相关资源
最近更新 更多