新手程序员的基本C++问题
Basic C++ question from a novice programmer
我是一名新手C++程序员,正在尝试在学校实验室工作。下面,我粘贴了我正在处理的程序的外壳。总的来说,我正在实例化属于class Velocity
的对象car1
。当我尝试将此实例化与函数 mpsConverter
一起使用时,我收到一个错误,指示表达式必须具有类类型。我们在课堂上做过类似的例子,这种格式工作得很好。有什么想法吗?如果这不是处理此类简单问题的合适论坛,请指出我正确的方向,以找到更合适的论坛。
谢谢,艾尔
// P1_2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "conio.h"
using namespace std;
class Velocity
{
private:
int mpsInput; // input value: meters per second (mps)
int kmphInput; // input value: km per hour
int mphOutput; // output value: comverted value of km per hour to miles per hour (mph)
public:
int kmphOutput; // output value: converted value of mps to km per hour
Velocity();
void mpsConverter(int speedKmph);
void mphConverter();
~Velocity();
};
Velocity::Velocity() // Constructor
{
cout << "The initial is text is displayed when an object in the class Velocity is Instantiated." << endl;
}
void Velocity::mpsConverter(int speedKmph) // convert KM per hour into meters per second (mps)
{
kmphOutput = (speedKmph * 2);
}
void Velocity::mphConverter() // convert KM per hour into miles per hour (mph)
{
}
Velocity::~Velocity() // Destructor
{
}
int main()
{
Velocity car1();
car1.mpsConverter(2);
getch();
return 0;
}
Velocity car1();
上述语句不是创建类型为 Velocity
的实例car1
。您正在尝试调用声明一个返回类型为 Velocity
的函数car1()
。由于没有创建实例 -
car1.mpsConverter(2); // This statement is giving you error stating mpsConverter(2)
// can only be called on class types.
Velocity car1 ; // This is the right way of instance creation.
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题