Error.. basic shape code

Started by
2 comments, last by Enigma 19 years, 4 months ago
Hi I am fairly new to C++ and wanted to make some shapes. I tried this code to make a rectangle but got 12 errors! int main () { class Shape { public: Move(int x, int y); SetColour(ColourType c); private: int xpos,ypos; ColourType colour; }; Shape::SetColour(ColourType c) { colour = c; } // terminate the program: return 0; } I put this into Draw1.cpp Any ideas of the correct code or which include files i may need. Thanks Mark.
Advertisement
You can't declare a class inside a function. I recommend you go through some C++ tutorials or buy a good book. Your code should look something like:

ColourType.h
// need to define ColourType before you can use itclass ColourType{	// something should go here, but only you can decide what};


Shape.h
#include "ColourType.h"class Shape{	public:		// specify a constructor to give the shape a default valid state		Shape();		// rename this function to make it clear whether it moves the shape		// TO a location or BY an amount		Move(int x, int y);		// nice to see that you spelt Colour right ;)		// prefer full names for variables - this is where the underscores		// explained below are useful		SetColour(ColourType colour);	private:		// advice to append an underscore to member variables to avoid naming		// conflicts with arguments to member functions		int xpos_;		int ypos_;		ColourType colour_;};


Shape.cpp
#include "Shape.h"Shape::Shape()	:	xpos_(),	ypos_(),	colour_(/*default colour should go here*/){}// rename this function to make it clear whether it moves the shape// TO a location or BY an amountShape::Move(int x, int y){	// some code to move the Shape	// probably either set or modify xpos_ and ypos_}Shape::SetColour(ColourType colour){	colour_ = colour;}


Main.cpp
#include "Shape.h"int main(){	// probably want to declare a Shape and do something with it here	// This is a completely unnecessary comment - terminate the program:	return 0;}


Enigma
Hi.

OK, I managed to fix the code, thanks. But now I discovered a Linking Error:

Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/DrawProg1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

DrawProg1.exe - 2 error(s), 0 warning(s)

Any ideas on how I can fix this?
Thanks

Mark
That linker error means that the linker can't find your main method. Main is the entry point for a console application and if the linker can't find it then it doesn't know how to start your program. You appear to only have one .cpp file that you're linking, yet my example had two: Shape.cpp and Main.cpp.

Enigma

This topic is closed to new replies.

Advertisement