简体   繁体   English

从C ++中的类指针对象调用方法(字符串)

[英]Calling a method from a class pointer object in C++ (strings)

The following code produces a runtime error, in which calling methods of the class produces no result. 以下代码产生运行时错误,该类的调用方法未产生结果。

MAIN.cpp : MAIN.cpp

#include "carClass.h"
#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

string VIN;
int miles;
string dealer;
int price;

int main(int argc, char** argv) {

    char command;
    ifstream infile;
    ofstream outfile;

    //Checks if the data file exists

    infile.open("base.txt", ifstream::in);
    outfile.open("base.txt", ios_base::app);

    cout << "Enter a command:" << endl;
    cin >> command;
    while (command != 'q') 
    {
        switch (command) 
        {       
            case 'a':
            {
                cin >> command;
                if (command == 'c') 
                {
                 //Gets user input
                    cin >> VIN >> miles >> dealer >> price;

                 //Creates new pointer variable with the user data
                    Car* vehicule = new Car(VIN, miles, dealer, price);
                    VIN=vehicule->getVin();                     
                    cout << "vehicule object VIN is: " << VIN;
                    //end of for loop
                }//end of if loop            
            }//end of case loop
            break;
        }//end of switch
        cout << "Enter a command:" << endl;
        cin >> command;
    }//end of while loop
    outfile.close();
    infile.close();
    return 0;
}

Here is where I try to call the method to my object pointer VIN=(*vehicule).getVin(); 在这里,我尝试在对象指针VIN=(*vehicule).getVin();调用该方法VIN=(*vehicule).getVin();

I also tried vehicule->getVin(); 我也尝试了vehicule->getVin(); and it did not work 而且没有用


Here is where I check if anything was stored and returns blank cout << "vehicule object VIN is: " << VIN; 在这里,我检查是否存储了任何内容,并返回空白cout << "vehicule object VIN is: " << VIN;

And if you wanna see here you have both source and header files 如果您想在这里看到源文件和头文件

carClass.cpp : carClass.cpp

#include "carClass.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

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 << this->mileage << endl <<
            this->dealership << endl << this->price << endl; 
    return;
}

carClass.h : carClass.h

#ifndef CAR_H
#define CAR_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 VIN;}
        int getMiles(){return mileage;}
        string getDealer(){return dealership;}
        int getPrice(){return price;}
        string getVinCode(){return vinCode;}

    private:
        string VIN;
        int mileage;
        string dealership;
        int price;
        string vinCode;
};  
#endif

The variables are being properly read from stdin . 可以从stdin中正确读取变量。 The problem is with your constructors though: 问题出在您的构造函数上:

Car::Car()
{
    string VIN; // local variable (this and all the other variables)!
    int mileage=0; 
    string dealership; 
    int price=0;
    string vinCode;
}

Car::Car(string vin, int miles,string carDealer, int dollars)
{
    string VIN=vin;  // local variable (this and all the other variables)! 
    int mileage=miles; 
    string dealership=carDealer; 
    int price=dollars;
    string vinCode = VIN.substr(0,3); 

}

In both of them you are using a local variable (ie its scope is the constructor's scope). 在这两种方法中,您都使用局部变量 (即,其范围是构造函数的范围)。 You should be using member variable instead, like so: 您应该改为使用成员变量 ,如下所示:

Car::Car()
{
    // Default constructor, no need to initialize VIN (though you may want to initialize it to some default value of course) 
    mileage=0; 
    price=0;
}

Car::Car(string vin, int miles,string carDealer, int dollars)
{
    VIN=vin; // VIN is a member variable 
    mileage=miles; 
    dealership=carDealer; 
    price=dollars;
    vinCode = VIN.substr(0,3); 
}

Change 更改

string VIN=vin;

to

VIN=vin;

in your Car constructor. 在您的Car构造函数中。 Otherwise you just write to a local variable instead of the class attribute. 否则,您只需写入局部变量而不是class属性。

The same applies to all other class attributes you are trying to set in that same constructor. 这同样适用于您试图在同一构造函数中设置的所有其他类属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM