cout/cin没有命名类型错误
cout/cin does not name a type error
继续接收此错误的代码有什么问题?
只有当我没有把"distance Formula"放在主要内容中,而是把它作为自己的类时,错误才会发生。
#include <iostream>
#include <string>
using namespace std;
class distanceFormula {
public:
int speed;
int time;
int distance;
cout << "What is the speed?" << endl;
cin >> speed;
cout << "How long did the action last?" << endl;
cin >> time;
distance = speed * time;
cout << "The distance traveled was " << distance << endl;
};
int main()
{
distanceFormula ao
ao.distanceFormula;
return 0;
};
类声明的主体只能包含成员,这些成员可以是数据或函数声明,也可以是访问说明符。
将代码封装在一个函数中,然后用对象在main
中调用它
class distanceFormula {
public:
int speed;
int time;
int distance;
void init()
{
cout << "What is the speed?" << endl;
cin >> speed;
cout << "How long did the action last?" << endl;
cin >> time;
distance = speed * time;
cout << "The distance traveled was " << distance << endl;
}
};
int main()
{
distanceFormula ao;
ao.init();
return 0;
};
如果您仍然需要使用一个类。以下是您的操作方法:
#include <iostream>
class distanceFormula
{
private:
int speed; // private
int time; // private
public:
distanceFormula(); // constructor
int getSpeed(); // to get
int getTime(); // to get
int getDistance(); // to get
void setSpeed(int); // to set
void setTime(int); // to set
};
distanceFormula::distanceFormula()
{
this->time = 0;
this->speed = 0;
}
int distanceFormula::getSpeed()
{
return this->speed;
}
int distanceFormula::getTime()
{
return this->time;
}
int distanceFormula::getDistance()
{
return this->time * this->speed;
}
void distanceFormula::setSpeed(int speedVal)
{
this->speed = speedVal;
}
void distanceFormula::setTime(int timeVal)
{
this->time = timeVal;
}
int main()
{
distanceFormula YourObject; // create obj
int SpeedValue;
int TimeValue;
std::cout << "Enter the Speed:";
std::cin >> SpeedValue; // take speed
std::cout << "Enter the Time:";
std::cin >> TimeValue; // take time
YourObject.setSpeed(SpeedValue); // set
YourObject.setTime(TimeValue); // set
std::cout << "This is the distance: " << YourObject.getDistance(); // retrieve result
getchar(); // wait
return 0;
}
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误