Error: aggregate value used where an integer was expected

N

Neviton

Help with this error ! Please !
The code below is simplified, because I'm trying to solve this error.

....MyClass.cpp In member function `void MyClass::Initialize()':
....MyClass.cpp aggregate value used where an integer was expected
....Makefile.win [Build Error] [MyClass.o] Error 1

The code:

/* MyClass.h *****************/
class MyClass
{
public:
void Initialize();
void MyFunction();
};

/* MyClass.cpp **************/
#include "MyClass.h"
void MyClass::Initialize()
{
long address = (long)MyFunction; //this line rises the error
}

void MyClass::MyFunction()
{
//empty
}
 
B

Barry

Neviton said:
Help with this error ! Please !
The code below is simplified, because I'm trying to solve this error.

...MyClass.cpp In member function `void MyClass::Initialize()':
...MyClass.cpp aggregate value used where an integer was expected
...Makefile.win [Build Error] [MyClass.o] Error 1

The code:

/* MyClass.h *****************/
class MyClass
{
public:
void Initialize();
void MyFunction();
};

/* MyClass.cpp **************/
#include "MyClass.h"
void MyClass::Initialize()
{
long address = (long)MyFunction; //this line rises the error

MyFunction is member function, not function

void MyClass::*pmf = &MyClass::MyFunction;

Moreover, casting a function pointer to other type is meaningless.
 
B

Barry

Barry said:
Neviton said:
Help with this error ! Please !
The code below is simplified, because I'm trying to solve this error.

...MyClass.cpp In member function `void MyClass::Initialize()':
...MyClass.cpp aggregate value used where an integer was expected
...Makefile.win [Build Error] [MyClass.o] Error 1

The code:

/* MyClass.h *****************/
class MyClass
{
public:
void Initialize();
void MyFunction();
};

/* MyClass.cpp **************/
#include "MyClass.h"
void MyClass::Initialize()
{
long address = (long)MyFunction; //this line rises the error

MyFunction is member function, not function

void MyClass::*pmf = &MyClass::MyFunction;

Sorry, what a impulsive man I am! deal too much with pointer to member
recently :)

void (MyClass::*pmf)() = &MyClass::MyFunction;
 
N

Neviton

How can I get the address of the function and save in a long variable.

My deal is use this WIN32 function that must receive the address of
function.

I know, I know
This is not a WIN32 Group but
I think my problem is with C++ instead WIN32.

I am wasting time for a few days and I can´t find a solution for my
problem.

If you can help me I will really appreciate

Thanks again
 
N

Neviton

When I try this in the main application class everything works fine.
The "aggregate value used where an integer was expected" error just
happen when I put the code in a Class.
 
B

Barry

Neviton said:
How can I get the address of the function and save in a long variable.

My deal is use this WIN32 function that must receive the address of
function.

I know, I know
This is not a WIN32 Group but
I think my problem is with C++ instead WIN32.

I am wasting time for a few days and I can´t find a solution for my
problem.

If you can help me I will really appreciate

If you have various kind of function which have different parameter
list, then you have to borrow Boost.Function, as my experience, I use
boost::function0<void> as wrapper, then bind any type of functor, free
function and member function into this wrapper.

If the parameter list is fixed, then you can write your own wrapper,
refer to 'mem_fun', 'bind2nd' ... in STL.
 
V

Victor Bazarov

Neviton said:
How can I get the address of the function and save in a long variable.

If that's a question, the answer is to use 'reinterpret_cast', but do
not rely on the pointer to fit into a 'long'. You may need to have
something different there, like 'uint64_t'.
My deal is use this WIN32 function that must receive the address of
function.

I know, I know
This is not a WIN32 Group but
I think my problem is with C++ instead WIN32.

Maybe. See FAQ 5.8.
I am wasting time for a few days and I can´t find a solution for my
problem.

Neither can we unless we actually see some code.

V
 
B

Barry

Neviton said:
When I try this in the main application class everything works fine.
The "aggregate value used where an integer was expected" error just
happen when I put the code in a Class.
Post your code to speak the error
 
J

Juha Nieminen

Neviton said:
How can I get the address of the function and save in a long variable.

My deal is use this WIN32 function that must receive the address of
function.

I don't even want to know why windows would want a function pointer as
a long (although having seen the Windows API I'm not even surprised
about anything anymore), but if if expects a pointer to a *function* and
you are giving a pointer to a *method* it won't work (at least if that
windows function tries to call your function through the pointer). A
method is not a regular function.
 
N

Neviton

Here is the code:

I'm getting this error
.... Button.cpp In member function `void Button::Create(HWND__*,
char*)':
.... Button.cpp aggregate value used where an integer was expected

/* Button.cpp ****************/
#include "Button.h"
void Button::Create(HWND hWindow, char * text)
{
Button::hWindow = hWindow;
handle = CreateWindow("Button", text, WS_CHILD | WS_VISIBLE, 10,
10, 100, 20, hWindow, (HMENU)0, NULL, NULL);
mainWindowProc = (WNDPROC) SetWindowLong(handle, GWL_WNDPROC,
(LONG) OwnerDrawButtonProc); // THE ERROR OCCUR HERE

}

HWND Button::Handle()
{
return handle;
}

LRESULT CALLBACK Button::OwnerDrawButtonProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
return TRUE;
}
return CallWindowProc(mainWindowProc, hWnd, uMsg, wParam, lParam);
}


/* Button.h ******************/
#include <windows.h>
class Button
{
public:
void Create(HWND hWindow, char * text);
HWND Handle();
private:
LRESULT CALLBACK OwnerDrawButtonProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
HWND hWindow;
HWND handle;
WNDPROC mainWindowProc;
};
 
B

Barry

Neviton said:
Here is the code:

I'm getting this error
... Button.cpp In member function `void Button::Create(HWND__*,
char*)':
... Button.cpp aggregate value used where an integer was expected

/* Button.cpp ****************/
#include "Button.h"
void Button::Create(HWND hWindow, char * text)
{
Button::hWindow = hWindow;
handle = CreateWindow("Button", text, WS_CHILD | WS_VISIBLE, 10,
10, 100, 20, hWindow, (HMENU)0, NULL, NULL);
mainWindowProc = (WNDPROC) SetWindowLong(handle, GWL_WNDPROC,
(LONG) OwnerDrawButtonProc); // THE ERROR OCCUR HERE

Make your member function("OwnerDrawButtonProc") static
 
V

Victor Bazarov

Neviton said:
Here is the code:

I'm getting this error
... Button.cpp In member function `void Button::Create(HWND__*,
char*)':
... Button.cpp aggregate value used where an integer was expected

/* Button.cpp ****************/
#include "Button.h"
void Button::Create(HWND hWindow, char * text)
{
Button::hWindow = hWindow;
handle = CreateWindow("Button", text, WS_CHILD | WS_VISIBLE, 10,
10, 100, 20, hWindow, (HMENU)0, NULL, NULL);
mainWindowProc = (WNDPROC) SetWindowLong(handle, GWL_WNDPROC,
(LONG) OwnerDrawButtonProc); // THE ERROR OCCUR HERE

Does it work if you simply pass 0?

Well, since we don't know what 'SegWindowLong' does, and what it
expects as its third argument, it's hard to tell what the solution
would be. However, it might help you to define 'OwnerDrawButtonProc'
as non-member, perhaps. Are you sure it's a static function in
your 'Button' class? If it isn't, you probably can't declare it
"CALLBACK" because callbacks usually don't have the instance of
the class to be called for. Search the FAQ for "callback".

V
 
J

James Kanze

How can I get the address of the function and save in a long variable.

If it's a non-static member function, you can't.
My deal is use this WIN32 function that must receive the address of
function.

What is the signature of the target function? I'm willing to
bet that it's something like:

void f( void (*)( void* ), void* ) ;

or even (more likely):

extern "C" void f( void (*)( void* ), void* ) ;

In neither case is a non-static member function acceptable, and
in the second case, even a static member function is not
acceptable.

If the pointer to the function is in fact passed using some data
type, e.g. a uint32_t, then you are skating on thin ice. You
must know how the function will be called. (`extern "C++"' or
`extern "C"', according to the standard, but VC++ has some
additional options), and only convert the address of a function
which can be called in that matter. In this case, the compiler
cannot help with the verifications. (And of course, a member
function is called in a far different manner than a non-member
function. The two are not compatible in any way.)
I know, I know This is not a WIN32 Group but I think my
problem is with C++ instead WIN32.

A bit of both. There are certainly C++ problems involved (and
Posix has similar problems).
 
J

James Kanze

I don't even want to know why windows would want a function pointer as
a long (although having seen the Windows API I'm not even surprised
about anything anymore), but if if expects a pointer to a *function* and
you are giving a pointer to a *method* it won't work (at least if that
windows function tries to call your function through the pointer). A
method is not a regular function.

For various reasons, OS's often have functions which violate
type safety. Regretfully, Windows isn't alone in this. (The
worst I've seen is dlsym, under Unix.)

And the problem isn't just (non-static) member vs. regular
function. Windows (or at least VC++) supports several different
call conventions; unless the function has the expected call
conventions, there's going to be problems at runtime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top