缺少显式类型C++(假定为"int")
C++ explicit type is missing('int' assumed)
嗨,我的 c++ 代码中有错误。我有 2 个.cpp文件和 1 个 .h 文件,我试图从头文件中访问 5 个字符串和 1 个 int,但我收到一个错误,说"缺少显式类型(假设为'int')。
我还有其他一些错误,它们是:缺少类型说明符,商店::项目重新定义;不同的基本类型,重载函数仅在返回类型上有所不同,声明不兼容。
这是我的文件:
用户选择.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
物品.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;
}
资料来源.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) 中缺少返回类型,根据您在头文件中的内容,这将是一个双精度。您遇到的其他错误很可能是相关的。
在您的类中拥有一个名为 main 的方法有点令人不安,因为它通常是用于程序入口点的函数名称。
您在 Items.cpp
中的定义中缺少返回类型:
double Shops::Items(string Item1, string Item2, string Item3, string Item4, string Item5, int Items)
{
//...
}
您还需要为 Shops::Items
和类的 main
函数返回一些值。
关于命名:在类中复制"正常"main
函数看起来很奇怪,并且有一个与类命名完全相同的参数看起来也很奇怪。它确实有效,但我会在代码审查 FWIW 中标记它。:)
相关文章:
- 为什么 int 类型的枚举类值不能用作 int
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 比较两个整数在C++中与未知 int 类型的相等性
- 二进制'operator+' 'int*'和'int*'类型的无效操作数
- 使用元编程选择 int 类型,将生成错误
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 复制初始化 - 从 'int' 类型转换为非标量类型
- 初始化cv :: mat ::具有int类型的定义大小的人
- 处理代码重新定义int类型(uint16_t,int16_t等),并不喜欢它
- 为什么没有"int"类型声明时输出不同?
- 在大型程序中将长变量更改为 int 类型
- 无法将 int 类型转换为时间类型(我的类类型)
- C++:无法在按位函数后将 int 类型设置为非标量
- 无法分配给INT类型的成员[10]
- C++ cin 将字符串读取到 int 类型中返回 0
- 为什么 decltype((i)) 是引用类型,而 decltype(i+0) 是 int 类型
- 使用4用于循环的4矩阵上的数组下标的“ int [int]类型”
- std::is_arithmetic 为通用 lambda 中的 int 类型返回 false:未定义的行为?
- 正在施放带有“ Int”类型的临时性