Struct inside namespace -- Not Member -- Need Fix?

B

Bryan Parkoff

MPU_Group is the namespace and MPU is the struct. Three members: A, B,
and C are in the MPU struct inside MPU_Group namespace. I tried to compile,
but it shows error saying MPU struct is not member in MPU_Group namespace.
Here is my code below. Please advise how to fix error?

// Load.h
namespace MPU_GROUP
{
struct _MPU
{
unsigned int A;
unsigned int B;
unsigned int C;
};
} // namespace MPU_GROUP

// Load.cpp
#include "Load.h"

namespace MPU_GROUP
{
_MPU MPU;
} // namespace MPU_GROUP

// Main.cpp
#include <stdio.h>
#include "Load.h"

void main (void)
{
MPU_GROUP::MPU.A = 0x41;

printf("Hello...\n");
}

Error Compiling
Load.cpp
Main.cpp
d:\test\main.cpp(6) : error C2039: 'MPU' : is not a member of 'MPU_GROUP'
d:\test\main.cpp(6) : error C2065: 'MPU' : undeclared identifier
d:\test\main.cpp(6) : error C2228: left of '.A' must have class/struct/union
type
Error executing cl.exe.

Test.exe - 3 error(s), 0 warning(s)

Bryan Parkoff
 
P

Peter MacMillan

Bryan said:
// Load.h
namespace MPU_GROUP
{
struct _MPU
{
unsigned int A;
unsigned int B;
unsigned int C;
};

extern _MPU MPU;
} // namespace MPU_GROUP

Because main.cpp can't see what is in Load.cpp you have to define MPU
somewhere (that main can "see" it). If you define it as an extern and
link in the Load.cpp file, everything goes fine.


C:\temp>cl Main.cpp Load.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

Main.cpp
Load.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:Main.exe
Main.obj
Load.obj


Cheers

--
Peter MacMillan
e-mail/msn: (e-mail address removed)
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
 
M

Malte Starostik

Bryan said:
MPU_Group is the namespace and MPU is the struct. Three members: A, B,
and C are in the MPU struct inside MPU_Group namespace. I tried to compile,
but it shows error saying MPU struct is not member in MPU_Group namespace.
Here is my code below. Please advise how to fix error?

// Load.h
namespace MPU_GROUP
{
struct _MPU
{
unsigned int A;
unsigned int B;
unsigned int C;
};
extern _MPU MPU;
// without this, the compiler only knows about MPU
// when compiling Load.cpp, but not Main.cpp
} // namespace MPU_GROUP

// Load.cpp
#include "Load.h"

namespace MPU_GROUP
{
_MPU MPU;
} // namespace MPU_GROUP

// Main.cpp
#include <stdio.h>
#include "Load.h"

void main (void)
int main()
// error: `main' must return `int'
// void main() is illegal and some compilers actually enforce it
{
MPU_GROUP::MPU.A = 0x41;

printf("Hello...\n");
}

After adding the extern declaration and fixing main, the code compiles fine.
 

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,772
Messages
2,569,593
Members
45,104
Latest member
LesliVqm09
Top