簡體   English   中英

缺少C ++顯式類型(假定為'int')

[英]C++ explicit type is missing('int' assumed)

嗨,我的C ++代碼有錯誤。 我有2個.cpp文件和1個.h文件,我試圖從頭文件訪問5個字符串和1個int,但是我收到一條錯誤消息,提示“缺少顯式類型(假定為'int')。

我也有其他一些錯誤:缺少類型說明符,Shops :: Items重定義; 不同的基本類型,重載的函數僅在返回類型和聲明不兼容方面有所不同。

這是我的文件:

UserChoice.h

#include <iostream>
#include <string>

using namespace std;

#ifndef USERCHOICE_H
#define USERCHOICE_H

class Shops
{
public:

    double Items(string, string, string, string, string, int);

    int main()
    {
        std::cout << newItem1;
    }

private:
    string newItem1;
    string newItem2;
    string newItem3;
    string newItem4;
    string newItem5;
    int newItems;

};
#endif

Items.cpp

#include "UserChoice.h"

Shops::Items(string Item1, string Item2, string Item3, string Item4, string Item5, int     Items)
{
    newItem1 = Item1;
    newItem2 = Item2;
    newItem3 = Item3;
    newItem4 = Item4;
    newItem5 = Item5;
    newItems = Items;
}

Source.cpp

#include "UserChoice.h";
#include <string>

int main()
{
    string Item1;
    string Item2;
    string Item3;
    string Item4;
    string Item5;
    int items;

    std::cout << "What what you like? Chicken, Meat, Fish, Carrot or Apple?\n";
    std::cin >> Item1;
    std::cout << "\nAnything else?\n";
    std::cin >> Item2;
    if(Item2 == "nothing else")
    {

    }
    std::cout << "\nAnything else?\n";
    std::cin >> Item3;
    std::cout << "\nAnything else?\n";
    std::cin >> Item4;
    std::cout << "\nAnything else?\n";
    std::cin >> Item5;
    std::cout << "\nAnything else?\n";
}

錯誤行

Shops::Items(string Item1, string Item2, string Item3, string Item4, string Item5, int Items)

所有代碼尚未完成,所以希望您能幫助我找到並修復這些錯誤。 在此先感謝您,如果您需要更多信息,請問我。

您缺少Shops :: Items的實現文件(cpp)中的返回類型,根據您的in頭文件中的內容,該類型將為double。 您遇到的其他錯誤很可能與您有關。

在類中有一個名為main的方法有點令人不安,因為它通常是用於程序入口點的函數名。

您在Items.cpp的定義中缺少返回類型:

double Shops::Items(string Item1, string Item2, string Item3, string Item4, string Item5, int Items)
{
    //...
}

您還需要為Shops::Items和類的main函數返回一些值。

關於命名:在類內部復制“正常” main函數看起來很奇怪,並且像類的命名一樣,命名參數也很奇怪。 它實際上確實有效,但是我會在代碼審查FWIW中對其進行標記。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM