• Advertisement
Sign in to follow this  

Undeclared Indentifier in C++

This topic is 4307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :(.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
#include

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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();
};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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_H

class test {
int x, y;
HBITMAP bitmap;
public:
void initBmp();
// ... declarations of methods to access the private fields in some way or another ...
};

#endif

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This should work:

In file main.cpp:
#include
#include "test.h"

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

In file test.h:
class test(){
int x,y;
void initBmp();
};

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

remember to rebuild the project...

Some comments:
I've used instead of " in the #include . This tell the compiler that window.h doesnt exists in the current folder. Using " , the compiler will first check in the current directory for the file berfore looking in the path. (or so I was told :) )

Regarding includes, only include header files.

As for the class test, you might want to make the initBitmap function public. Or perhaps call it in the constructor.
I use the following format for classes:

class name
{
public:
// public member functions

protected:
// protected member functions

private:
// private member functions

// private member variables.
};

Although not every field is used. It depends on what type of class I want.

The HBITMAP is a local variable, it will be lost as the function initBitmap exits. You might want to store it in the class:

File test.h

class test
{
public:
test();

// as the x and y are private, you will need getter for them...
int getX() { return m_x; } // inline functions. They can also be declared
int getY() { return m_y; } // in test.cpp as int test::getX() { return m_x; }

// and setters
void setX(int x);
void setY(int y);

private:
void initBitmap();
int m_x, m_y;
HBITMAP m_hBitmap;
};

File test.cpp

#include "test.h"
test::test()
{
initBitmap();
}

void test::setX(int x)
{
// perhaps add a check to see if x is within some boundary
m_x = x;
}

void test::setY(int y)
{
m_y = y;
}

void test::initBitmap()
{
// do something with the handle
m_hBitmap = get_a_handle_to_a_bitmap; // insert you bitmap loading here...
}

Hehe, this post became longer than I expected. Anyway, hope this helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hehe, the last 3-4 replies was posted while I was typing mine :)

Share this post


Link to post
Share on other sites
hehehe.. thanks for the reply, now I understand about class, I'll try that source again later, because i can't found vc++ on this computer :(.
Can anyone give some links about windows GDI with c++ tutorial for n00b like me?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement