VS一直给我一个类项目的每个函数标题中提到的错误(至少是Vehicle.h中的所有函数),无论我怎么努力,我都无法弄明白。它似乎不是由于任何循环定义,但也许答案很简单。该项目应该基于四个文件(两个头文件和两个.cpp' s);我将它们附在下面。
Vehicle.h:
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
using namespace std;
//Declaring Dealer class
class Dealer
{
public:
Dealer();
int getDealerNum();
void setDealerNum(int dealerNumber);
private:
int dealerNumber;
Dealer *dealer;
};
//Declaring Vehicle class
class Vehicle
{
public:
Dealer *dealerType;
Vehicle(string VIN, string make, string model, int year, double price);
Vehicle();
string getVIN();
string getMake();
string getModel();
int getYear();
double getPrice();
void setVIN(string VIN);
void setMake(string make);
void setModel(string model);
void setYear(int year);
void setPrice(double price);
friend Vehicle;
private:
string VIN;
string make;
string model;
int year;
double price;
};
Functions.h(仅包含一个函数,以节省一些空间):
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include<string>
#include<vector>
using namespace std;
//Declaring name for input and output file
ofstream outfile;
ifstream infile;
void displayInventory(vector<Vehicle>& vehicles)
{
int i = 0;
char query = 'a';
int j = vehicles.size();
//Simple loop to display the inventory, with a pause function to wait for user exit
while (query != 'x'&&query!='X')
{
for (i; i < j; i++)
{
cout << "Vehicle #" << i + 1 << endl;
cout << "Vin: " << vehicles[i].getVIN << endl;
cout << "Make: " << vehicles[i].getMake << endl;
cout << "Model: " << vehicles[i].getModel << endl;
cout << "Year: " << vehicles[i].getYear << endl;
cout << "Price: $" << vehicles[i].getPrice << endl << endl;
}
cout << endl << "Enter 'x' to return to main menu" << endl;
cin >> query;
cout << endl;
}
}
Main.cpp的:
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include "Thomas-PA3 Functions.h"
#include "Thomas-PA3 Vehicle.h"
using namespace std;
void displayInventory(vector<Vehicle>& vehicles);
void addInventory(vector<Vehicle>& vehicles);
void deleteInventory(vector<Vehicle>& vehicles);
void updateInventory(vector<Vehicle>& vehicles);
void sortInventory(vector<Vehicle>& vehicles);
void searchInventory(vector<Vehicle>& vehicles);
void writeInventory(vector<Vehicle>& vehicles);
void refreshInventory(vector<Vehicle>& vehicles);
int main()
{
char menu='0';
vector<Dealer>dealers;
vector<Vehicle>vehicles;
while(menu!='8')
{
cout << "Welcome to the vehicle management menu." << endl;
cout << "Please select an option from the following list:" << endl;
cout << "1: Display vehicles" << endl;
cout << "2: Add a vehicle" << endl;
cout << "3: Update a vehicle" << endl;
cout << "4: Delete a vehicle" << endl;
cout << "5: Sort inventory by VIN" << endl;
cout << "6: Search inventory by Make" << endl;
cout << "7: Read inventory from file (will overwrite any changes)" << endl;
cout << "8: Write inventory to file and exit" << endl;
cin >> menu;
switch(menu)
{
// Call to appropriate functions based upon user decision
case '1': displayInventory(vehicles);
break;
case '2': addInventory(vehicles);
break;
case '3': updateInventory(vehicles);
sortInventory(vehicles);
break;
case '4': deleteInventory(vehicles);
break;
case '5': sortInventory(vehicles);
break;
case '6': searchInventory(vehicles);
break;
case '7': refreshInventory(vehicles);
break;
case '8': sortInventory(vehicles);
writeInventory(vehicles);
break;
//Error/incorrect input checking
default: cout << endl << "Please make a valid selection" << endl << endl;
}
}
return 0;
}
Vehicle.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
#include "Thomas-PA3 Vehicle.h"
#include "Thomas-PA3 Functions.h"
using namespace std;
//Default constructor for Dealer
Dealer::Dealer()
{
dealerNumber = 0;
}
//Function to get dealer number
int Dealer::getDealerNum()
{
return dealerNumber;
}
void Dealer::setDealerNum(int dealerNumber)
{
cout << "Please input the new dealer number" << endl;
cin >> dealerNumber;
}
//Custom constructor for Vehicle
Vehicle::Vehicle(string VIN, string make, string model, int year, double price)
{
cout << "Vehicle data is initialized" << endl;
}
//Default constructor for Vehicle
Vehicle::Vehicle()
{
VIN = "0";
make = "";
model = "";
year = 0;
price = 0;
}
//Functions to return member variables
string Vehicle::getVIN()
{
return VIN;
}
string Vehicle::getMake()
{
return make;
}
string Vehicle::getModel()
{
return model;
}
int Vehicle::getYear()
{
return year;
}
double Vehicle::getPrice()
{
return price;
}
//Functions to set member variables
void Vehicle::setVIN(string VIN)
{
cout << "Please input the vehicle's VIN #" << endl;
cin >> VIN;
}
void Vehicle::setMake(string make)
{
cout << "Please input the vehicle's make" << endl;
cin >> VIN;
}
void Vehicle::setModel(string model)
{
cout << "Please input the vehicle's model" << endl;
cin >> VIN;
}
void Vehicle::setYear(int year)
{
cout << "Please input the vehicle's year" << endl;
cin >> VIN;
}
void Vehicle::setPrice(double price)
{
cout << "Please input the vehicle's price" << endl;
cin >> VIN;
}
答案 0 :(得分:1)
在“Vehicles.h”之前加入“Functions.h”。因此,当编译器看到vector<Vehicle>
时,它还不知道稍后会将Vehicle
定义为类。
C ++分三个阶段编译。首先,预处理器运行并执行#include
语句等。这是每个.cpp文件完成一次,结果被送到真正的编译器。然后编译器逐行编译此结果。最后,链接器将所有内容粘合在一起。
这里的重要阶段是中间阶段。因为每个.cpp文件是从上到下隔离编译的,所以将必要的头文件放在顶部。如果一个标题需要另一个,你可以将一个标题放在另为了简化这一点,#include
在另一个标题中有一个标题会很有用。毕竟,所有#include
语句都已执行。一切最终都会在.cpp文件中结束。
最后一句话:你通常需要保护自己免受双重#include
的攻击。还有第二种预处理器机制:使用
#ifndef VEHICLE_H
#define VEHICLE_H
// Real contents of Vehicle.h go here, including any other #include statement
#endif
如果你现在写
#include "vehicle.h"
//.. other stuff
#include "vehicle.h"
预处理器将看到第二个语句,并注意VEHICLE_H
已经定义,因此不需要第二个包含。拼写注意:它可以防止与class Vehicle
和其他类似名称混淆,但你不能使用.
,因此后缀为_H
。