Undeclared Indentifier in C++

Started by
11 comments, last by kecebongsoft 18 years ago
I am a newbie on C++ Programming, am trying to declare HBITMAP in other file (test.cpp), but i've got message "HBITMAP undeclare identifier", here's the source : FILE main.cpp #include "windows.h" #include "test.cpp" int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ return 0; } FILE test.cpp void test(){ HBITMAP hBmp; } Well, what's wrong with my code?, i try to place #pragma comment(lib,"gdi32.dll") and #include "windows.h" in test.cpp, but i've got no better :(.
Advertisement
#include
You need to include windows.h in test.cpp.
more detail please...
to roboguy:
I was put #include "windows.h" in test.cpp, but i've got message that tell me void test already declared in main.obj. Can anyone give me some samples please?
Remove your #include "test.cpp" from your other source.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

if I remove #include "test.cpp" in main.cpp, i'll get error(undeclared identifier) when I make a class in test.cpp and declared it in main.cpp, my original source is this :

FILE main.cpp
#include "windows.h"
#include "test.cpp"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
test myBitmap;
return 0;
}


FILE test.cpp
#include "test.h"
void test::initBmp{
HBITMAP hBmp;
}

FILE test.h
class test(){
int x,y;
void initBmp();
};
You shouldn't include cpp files, you should compile them separately. Finally "linker" will link all the separately compiled cpps into one exe or dll or whatever. main.cpp should include test.h, not test.cpp. That way it'll know about the class called "test" and not complain about undeclared identifier.
Quote:Original post by kecebongsoft
if I remove #include "test.cpp" in main.cpp, i'll get error(undeclared identifier) when I make a class in test.cpp and declared it in main.cpp, my original source is this :

FILE main.cpp
#include "windows.h"
#include "test.cpp"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
test myBitmap;
return 0;
}


FILE test.cpp
#include "test.h"
void test::initBmp{
HBITMAP hBmp;
}

FILE test.h
class test(){
int x,y;
void initBmp();
};


Well, first of all, your syntax isn't quite right. You probably meant something like this (there are a few points which are a bit unclear, so I'll have to guess a bit):
// main.cpp#include <windows.h>#include "test.h"int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {    test testBitmap;    return 0;}


// test.cpp#include "test.h"void test::initBmp() {    // ... do something with bitmap here ...}// ... definitions of methods to access the private fields in some way or another ...


// test.h#ifndef TEST_H#define TEST_Hclass test {    int x, y;    HBITMAP bitmap;public:    void initBmp();    // ... declarations of methods to access the private fields in some way or another ...};#endif
Quote:Original post by kecebongsoft
I am a newbie on C++ Programming, am trying to declare HBITMAP in other file (test.cpp), but i've got message "HBITMAP undeclare identifier", here's the source :

FILE main.cpp
#include "windows.h"
#include "test.cpp"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
return 0;
}


FILE test.cpp

void test(){
HBITMAP hBmp;
}

Well, what's wrong with my code?, i try to place #pragma comment(lib,"gdi32.dll") and #include "windows.h" in test.cpp, but i've got no better :(.


EDIT: removed by xeddiex...

- xeddiex
one..

This topic is closed to new replies.

Advertisement