2015-09-13 118 views
0

以下代碼會生成運行時錯誤,其中該類的調用方法不會生成任何結果。從C++中的類指針對象調用方法(字符串)

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

這裏是我嘗試調用方法我對象的指針 VIN=(*vehicule).getVin();

我也試過vehicule->getVin();並沒有奏效


這裏我在哪裏檢查是否存儲了任何內容並返回空白 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 

回答

3

變量正在從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); 
} 
+0

謝謝,現在向上投票。 – mastov

1

變化

string VIN=vin; 

VIN=vin; 

Car構造。否則,你只需寫入一個局部變量而不是class屬性。

這同樣適用於您嘗試在同一構造函數中設置的所有其他類屬性。