首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘'Vehicle’不是参数'_Ty‘的有效模板类型参数

‘'Vehicle’不是参数'_Ty‘的有效模板类型参数
EN

Stack Overflow用户
提问于 2016-04-28 20:38:50
回答 1查看 1.6K关注 0票数 0

VS总是给我一个类项目的每个函数的标题中提到的错误(至少是Vehicle.h中的所有函数),而且无论我多么努力,我都找不出它。这似乎不是由于任何循环定义,但也许答案很简单。该项目应该基于四个文件(两个头文件和两个..cpp文件);我将在下面附加它们。

车辆h:

代码语言:javascript
复制
    #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;
};

H(只包含一个函数,以节省一些空间):

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-28 21:55:13

在"Vehicles.h“之前包括"Functions.h”。因此,当编译器看到vector<Vehicle>时,它还不知道以后Vehicle将被定义为一个类。

C++分三个阶段编译。首先,预处理程序运行并执行#include语句等。这在每个.cpp文件中完成一次,并将结果提供给真正的编译器。然后编译器逐行编译此结果。最后,链接器将所有的东西粘合在一起。

这里的重要阶段是中间阶段。因为每个.cpp文件都是自上而下独立编译的,所以您可以将必要的头放在顶部。如果一个头需要另一个,你就把一个放在另一个上面。为了简化这一点,可以在另一个头中使用#include。毕竟,所有的#include语句都是执行的。最终,一切都会在.cpp文件中结束。

最后一点:您通常需要保护自己不受双重#include的影响。

代码语言:javascript
复制
#ifndef VEHICLE_H
#define VEHICLE_H

// Real contents of Vehicle.h go here, including any other #include statement

#endif

如果你现在写

代码语言:javascript
复制
#include "vehicle.h"
//.. other stuff
#include "vehicle.h"

预处理器将看到第二个语句,并注意已经定义了VEHICLE_H,因此不需要第二个包含。拼写注意:为了防止与class Vehicle和其他类似名称混淆,您不能在那里使用.,所以后缀是_H

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36925041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档