By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Bryan Parkoff wrote:
// 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: pe***@writeopen.com
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(--)
Jul 23 '05 #2

P: n/a
Bryan Parkoff schrieb:
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.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.