Basic C++ question from a novice programmer -
i novice c++ programmer , trying work on school lab. below, have pasted shell of program working on. in main, instantiating object car1
belongs class velocity
. when try use instantiation function mpsconverter
, error indicating expression must have class type. have done similar examples in class , format worked fine. ideas? if not appropriate forum simple questions this, please point me in right direction more appropriate one.
thanks, al
// p1_2.cpp : defines entry point 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 miles per hour (mph) public: int kmphoutput; // output value: converted value of mps km per hour velocity(); void mpsconverter(int speedkmph); void mphconverter(); ~velocity(); }; velocity::velocity() // constructor { cout << "the initial text displayed when object in class velocity instantiated." << endl; } void velocity::mpsconverter(int speedkmph) // convert km per hour meters per second (mps) { kmphoutput = (speedkmph * 2); } void velocity::mphconverter() // convert km per hour miles per hour (mph) { } velocity::~velocity() // destructor { } int main() { velocity car1(); car1.mpsconverter(2); getch(); return 0; }
velocity car1();
the above statement isn't creation of instance car1
of type velocity
. trying call declare function car1()
return type velocity
. since there no instance created -
car1.mpsconverter(2); // statement giving error stating mpsconverter(2) // can called on class types. velocity car1 ; // right way of instance creation.
Comments
Post a Comment