473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Question concerning .NET 2003 and Namespaces

Recently I've come across an oddity in .NET, going back to a Managed
C++ implementation for GUIs since I never really learned how to code in
MFC.

Any way, I attempted to put some standard code I often re-use (like my
own random function) in its own namespace, lets call this namespace
"PersonalStd", and noticed that when I tried to reference the function
`random()' in the PersonalStd namespace from the project namespace it
was giving me a LNK2001 error despite everything being included in the
project.

It works fine with namespaces when compiled in unmanaged C++ compilers,
like MSVC6, unfortunately I haven't had a chance to test it on Linux
compilers, but I'm pretty certain it would work fine on those as well.

I think its a conflict with how managed C++ in .NET deals with
additional namespaces, because because when I took the PersonalStd
namespace out of the code, everything worked fine. I'm just wondering
if anyone else has run into this, or if anyone has insight or
suggestions into this?

Oh and as a side note; if anyone knows some really good MFC resources,
tutorials, or books, please let me know -- its sort of been something
that has been eluding me for a few years now that I just never really
tried to learn, but I feel like I should.

An example of what the code looked like with the namespaces is here:

--- PersonalStd.h

#pragma once
#ifndef PERSONAL_STANDARD
#define PERSONAL_STANDARD 1

namespace PersonalStd
{
int random(int, int);
}

#endif
--- PersonalStd.cpp

#include <algorithms>
#include <cmath>
#include "PersonalStd.h"

int random(int l, int h)
{
....
}
--- MainCode.h (where all the functions are in managed C++)

....

System::Void SomeFunction(...)
{
txtBox1->Text = PersonalStd::random(1,10);
}

Jul 23 '05 #1
4 1262
Sean Quinn wrote:
Recently I've come across an oddity in .NET, going back to a Managed
C++ implementation for GUIs since I never really learned how to code in
MFC.
This newsgroup has no dealings with .NET, Managed C++, or MFC. Just to
let you know...
Any way, I attempted to put some standard code I often re-use (like my
own random function) in its own namespace, lets call this namespace
"PersonalStd", and noticed that when I tried to reference the function
`random()' in the PersonalStd namespace from the project namespace it
was giving me a LNK2001 error despite everything being included in the
project.
Of course. You didn't define that function. See below.
It works fine with namespaces when compiled in unmanaged C++ compilers,
It does? I just tried to build this code:
------------------
namespace A {
int foo();
}

int foo() { return 42; }

int main() {
return A::foo();
}
-------------------
and A::foo was reported as unresolved external, as I expected.
like MSVC6,
MSVC6 is notoriously bad in many aspects. Upgrade your compiler.
unfortunately I haven't had a chance to test it on Linux
compilers, but I'm pretty certain it would work fine on those as well.

I think its a conflict with how managed C++ in .NET deals with
additional namespaces [...]
If you need suggestions on "managed C++" or ".NET", you should consider
asking in a newsgroup whose name begins with "microsoft.public.".
Oh and as a side note; if anyone knows some really good MFC resources,
[...]
Somebody in an MFC newsgroup should be able to help you with that.
An example of what the code looked like with the namespaces is here:

--- PersonalStd.h

#pragma once
#ifndef PERSONAL_STANDARD
#define PERSONAL_STANDARD 1

namespace PersonalStd
{
int random(int, int);
}

#endif
--- PersonalStd.cpp

#include <algorithms>
#include <cmath>
#include "PersonalStd.h"

int random(int l, int h)
This declaration is not for the function in the namespace. It's for its
own function in the global namespace (AFA C++ is concerned). And that
means that 'PersonalStd::random' function has no definition, which is
against the ODR.
{
...
}
Surround this declaration/definition with the namespace and you will rid
yourself of the error, I believe.


--- MainCode.h (where all the functions are in managed C++)

...

System::Void SomeFunction(...)
{
txtBox1->Text = PersonalStd::random(1,10);
}


V
Jul 23 '05 #2
Victor Bazarov wrote:
This newsgroup has no dealings with .NET, Managed C++, or MFC. Just to
let you know...
Thanks, for future questions I have on Managed, or MFC I will check
there, I appreciate your answers though, regardless!

Surround this declaration/definition with the namespace and you will rid
yourself of the error, I believe.


You're absolutely right, I don't know why I didn't catch that (I think
I've just been exhausted), thank you for pointing that out to me, its
so embarassing when one makes a mistake like that!

Thank you again!

Jul 23 '05 #3
Sean Quinn wrote:
It works fine with namespaces when compiled in unmanaged C++ compilers,
like MSVC6, unfortunately I haven't had a chance to test it on Linux
compilers, but I'm pretty certain it would work fine on those as well.
VC .NET 2003 compiler can quite happily do unmanaged C++.

I suspect that your code is not compliant to the C++ standard, and that MSVC6 just didn't catch that (since it's not very compliant).
Oh and as a side note; if anyone knows some really good MFC resources,
tutorials, or books, please let me know -- its sort of been something
that has been eluding me for a few years now that I just never really
tried to learn, but I feel like I should.


Why don't you use the new fangled .NET GUI API rather than MFC?

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #4
> VC .NET 2003 compiler can quite happily do unmanaged C++.

I suspect that your code is not compliant to the C++ standard, and that MSVC6 just didn't catch that (since it's not very compliant).


Yeah I know it can (I've done it before a while back, maybe 4 or 6
months ago, just haven't touched .NET recently and for some reason
became stupid in that time frame I think), but the real problem was
that I was giving hypothetical example code thus running into the same
problem that I was running into with the actual code I was writing.
Essentially forgetting to declare the namespace scope before the
function name, for some reason I was thinking "using namespace X" was
enough to define the scope, when in previous projects of mine, I knew
it wasn't. It was just a stupid moment. heh.
Oh and as a side note; if anyone knows some really good MFC resources,
tutorials, or books, please let me know -- its sort of been something
that has been eluding me for a few years now that I just never really
tried to learn, but I feel like I should.


Why don't you use the new fangled .NET GUI API rather than MFC?


Its not that I don't want to use the .NET GUI (I love it actually, a
heck of a lot easier the MFC if I recall my struggles with MFC earlier
on correctly), but MFC has sort of been that elusive beast that I've
been trying to catch, and would like to at least become familiar with
it for that reason alone.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
6
by: jdph40 | last post by:
We recently had to upgrade the computers in our company. Now our office's website on our company intranet no longer recognizes reports saved in snapshot format. We get an error message that the...
5
by: EMonaco | last post by:
All, I have a simple C# window app under MyApp namespace it has a class Form1. I've added another class .cs file. Now this class I want to share with many projects, so I figured I'd change this...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
1
by: Leon | last post by:
I'm Creating multiple custom unique namespaces within the same web application. Using VS.net do I still have to compile each namespace into an assembly separately using the vb.net 2003 command...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.