Question:
What am I doing wrong in this C++ Class?
Vausch
2012-01-29 21:24:21 UTC
// Class RoomType cpp : Defines the entry point for the console application.
//
class RoomType
{
// Data delcaration section
private:
double length; // declare length as a double variable
double width; // declare width as a double variable

//methods declaration section
};
#include "stdafx.h"
#include
using namespace std;
int main()
{
RoomType roomOne;

cin.ignore();
return 0;
}

It gives me 3 errors: "RoomType"; undeclared identifier,
syntax error, Missing ";" before 'roomOne"
"roomOne" undeclared identifier.

I'm told I need to put objects into the length and width but I have no idea how to do so. Thanks.
Four answers:
D Bügger™
2012-01-29 21:33:24 UTC
You program will compile after minor changes.The problem is in the form of your program.GNU C++ compiler will compile this but it won't compile on Microsoft Visual C++ without using the right format.Although individual styles will differ, most C++ programs will have this general form:

#includes //all the macros come here in the top section of the code

base-class declarations

derived class declarations

nonmember function prototypes

int main( )

{

//...

}

nonmember function definitions

In most large projects, all class declarations will be put into a header file and included

with each module. But the general organization of a program remains the same.



so your program should look like this:--

#include "stdafx.h"

#include

using namespace std;



// Class RoomType cpp : Defines the entry point for the console application.

//

class RoomType

{

// Data delcaration section

private:

double length; // declare length as a double variable

double width; // declare width as a double variable



//methods declaration section

};



int main()

{

RoomType roomOne;



cin.ignore();

return 0;

}



This will compile and run but for some output you will have to add more lines.You said,"I'm told I need to put objects into the length and width but I have no idea how to do so".I too don't understand that.When you have to call the length and breadth variables in main function you must use the object of the class roomType which is roomOne and is declared here:-

RoomType roomOne;



Syntax for calling length and breadth for user input:-

cin>>roomOne.length;

cin>>roomOne.breadth;
husoski
2012-01-29 22:39:34 UTC
That Microsoft-specific "stdafx.h" garbage is the cause. That get's generated by default with Visual C++ projects. Also, you get a Windows-specific tmain() entry point instead of the standard main().



The simplest way to fix it is to move the RoomType class declaration *after* #include "stdafx.h".



The best way to fix it is to recreate your project. On the second screen of the project wizard for a "C++ Console Application", you have an "Application Settings" tab on the left. Click that and then, (a) turn off "Precompiled headers", and then select "Empty project".



Then, you can create just what source files you need (no stdafx.h or stdafx.cpp) and you don't get the nonstandard "Hello World" program.



The reason that you got that bogus error is that the precompiled header (PCH) feature assumes that every source file in the project has identical definitions up to and including the include of "stdafx.h". It sets up your project to compile a dummy file (stdafx.c or cpp) just once to create the precompiled header file, and that file is use for all definitions up to and including "stdafx.h". If programs have different orders of definitions and includes in the "precompiled" part, it causes problems like yours.



The PCH feature can be a timesaver for compiling huge projects, especially for Windows apps, where the "windows.h" file brings a ton of definitions into every source that's compiled. You don't need it for small projects.
iamurfriend
2012-01-29 21:37:03 UTC
Your program doesn't have any issues. I tried compiling using VC++ 9.0 and it is not giving any errors.

Just i removed #include "stdafx.h" and tried.



By message...

All the 3 error messages indicates the same actually. The class not defined or linked. You should not face any issues if all these code lines are in a cpp file.
pals
2016-10-05 01:42:39 UTC
that's incorrect for her to contemplate the paper crap because of the fact she would not believe in evolution. yet you properly known that the paper substitute into crap, so . . . She should not be a technological expertise instructor if she would not believe in technological expertise.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...