PDA

View Full Version : [C++] Some errors :(


Zegg90
06-16-2006, 09:40 PM
I'm trying to compile my program, just a simple one that works with the console...
I'm getting this error though:
"new types may not be defined in a return type"

Here's the code where the error occurs on:

/* 'ConLib.cpp' */

/* ConLib complement header file */
#include "ConLib.h"

/* Get standard screen and keyboard handles */
ConLib::ConLib()
{
m_Screen = GetStdHandle(STD_OUTPUT_HANDLE);
m_Keyboard = GetStdHandle(STD_INPUT_HANDLE);

SetTextColor(ConRed | ConGreen | ConBlue);
SetBackgroundColor(0);
} //ConLib()

It happens on this line:
{

(Line 8)


Can somebody please help? I've searched google, and the only solution I found was a missing semi-colon... But no semi-colon is needed there! (Or is it really needed?)

rpgfan3233
06-17-2006, 03:53 AM
Since you are using the Win32 API, I came up with the following to recreate it. I think it is pretty much self-explanatory:

// ConLib.cpp

#include "ConLib.h"

ConLib::ConLib () {
bgColour = 0;
textColour = 0;
m_Screen = GetStdHandle(STD_OUTPUT_HANDLE);
m_Keyboard = GetStdHandle(STD_INPUT_HANDLE);

SetTextColor(ConRed | ConGreen | ConBlue);
SetBackgroundColor(0);
}

ConLib::~ConLib () {
SetTextColor(ConRed | ConGreen | ConBlue);
SetBackgroundColor(0);
}

void ConLib::SetTextColor (int TextColour) {
SetConsoleTextAttribute(m_Screen, bgColour * 16 + TextColour);
textColour = TextColour;
}

void ConLib::SetBackgroundColor (int BGColour) {
SetConsoleTextAttribute(m_Screen, BGColour * 16 + textColour);
bgColour = BGColour;
}


// ConLib.h

#ifndef __CONLIB_H__
#define __CONLIB_H__
#endif

#include <windows.h>

#define ConBlue 1
#define ConGreen 2
#define ConRed 4
#define ConAlpha 8

class ConLib { // ConLib class
public:
ConLib ();
~ConLib ();
void SetTextColour ( int );
void SetBackgroundColour ( int );
private:
int bgColour; // added for storing background color value
int textColour; // added for storing text color value
HANDLE m_Screen;
HANDLE m_Keyboard;
};


// ConLibTest.cpp

#include "ConLib.h"
#include <cstdio>

int main () {
ConLib test;
test.SetBackgroundColor(ConRed);
printf("Hello World!\n");
return 0;
}

Zegg90
06-17-2006, 08:15 PM
Thanks...

What was the original problem though?

rpgfan3233
06-18-2006, 12:42 AM
Perhaps you forgot to define ConLib() in your header file when you used ConLib::ConLib() in your CPP file? I don't know what happened, but it works. :D

Zegg90
06-18-2006, 05:49 PM
You mean these lines?

#ifndef __CONLIB_H__
#define __CONLIB_H__
#endif

rpgfan3233
06-18-2006, 09:05 PM
No, in the header file, I defined
class ConLib{
public:
ConLib();
....

and in the CPP file:
ConLib::ConLib() ...


Perhaps you didn't do the header file part?

Edit: I figured it out. You forgot the semicolon at the end of your class definition:
class ConLib {
// code here
};

Zegg90
06-24-2006, 04:06 PM
Do I need the semi-colon at the end of every } sign?

Which places do I need a semi-colon? I thought only for single lines of code, not code blocks...

rpgfan3233
06-24-2006, 09:25 PM
You need it at the end of a class, a structure, a union or an enumerated type (enum). Here is some C code which basically does the same thing:


typedef struct conlibtype {
/* stuff here */
} ConLib; /* name of the user defined type */


C or C++, semicolons are required.