gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:56:28 25 4
gpt4 key购买 nike

以下代码会产生一个运行时错误,其中类的调用方法不会产生任何结果。

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();

I also tried 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;

如果您想在这里看到源文件和头文件

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 :

#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

最佳答案

正在从 stdin 正确读取变量。问题在于您的构造函数:

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);

}

在它们中你都使用了一个局部变量(即它的作用域是构造函数的作用域)。您应该改用成员变量,如下所示:

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);
}

关于c++ - 从 C++ 中的类指针对象调用方法(字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com