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

"using namespace std" in VS C++ Class Library (.NET) project

P: n/a
When I create a new C++ project of type "Class Library (.NET)", I am
unable to then add the following line of code :
using namespace std

If I do, I get the following error on compilation :
c:\Research\Code\Visual Studio\TestC++2\TestC++2.h(7): error
C2871: 'std' : a namespace with this name does not exist

If I try this with other project types, e.g. "Console Application
(.NET)" projects, I have no such problem.

Any ideas on what I can do to fix this (as I would like to use
functions from <string.h> in my code).

Thanks,

Alex.
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
AlexD_UK wrote:
When I create a new C++ project of type "Class Library (.NET)", I am
unable to then add the following line of code :
using namespace std

If I do, I get the following error on compilation :
c:\Research\Code\Visual Studio\TestC++2\TestC++2.h(7): error
C2871: 'std' : a namespace with this name does not exist

If I try this with other project types, e.g. "Console Application
(.NET)" projects, I have no such problem.

Any ideas on what I can do to fix this (as I would like to use
functions from <string.h> in my code).
Use #include<string> rather than <string.h>

Thanks,

Alex.

--
Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com
Nov 17 '05 #2

P: n/a

"Jacobo Rodriguez Villar" <skdjf@sdkjf> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
AlexD_UK wrote:
When I create a new C++ project of type "Class Library (.NET)", I am
unable to then add the following line of code :
using namespace std

If I do, I get the following error on compilation :
c:\Research\Code\Visual Studio\TestC++2\TestC++2.h(7): error
C2871: 'std' : a namespace with this name does not exist

If I try this with other project types, e.g. "Console Application
(.NET)" projects, I have no such problem.

Any ideas on what I can do to fix this (as I would like to use
functions from <string.h> in my code).


Use #include<string> rather than <string.h>

Thanks,

Alex.

--
Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com

Actually, #include <string> will get you the C++ string class, not what is
in <string.h>.
What you want is

#include <cstring>

which is the c++ equivalent of string.h

Nov 17 '05 #3

P: n/a
Thanks to you both.

When I try the first option with new Class Library (.NET) project and
the following code :

// TestC5.h
#pragma once
#include <string>
using namespace System;

namespace TestC5
{
public __gc class Class1
{
};

void method(void)
{
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);
}
}

I get this error :
TestC++5 error LNK2020: unresolved token (0A000006) _CxxThrowException
TestC++5 error LNK2020: unresolved token (0A000017) delete

If I substitute for #include <cstring>, I get :
TestC++5 error LNK2001: unresolved external symbol "char * __cdecl
strncpy(char *,char const *,unsigned int)"
(?strncpy@@$$J0YAPADPADPBDI@Z)
Am I missing another import library?

Alex.
Nov 17 '05 #4

P: n/a
Nope, strncpy is in the runtime, not import libraries
--
cheers,
Costea Bogdan
"AlexD_UK" wrote:
Thanks to you both.

When I try the first option with new Class Library (.NET) project and
the following code :

// TestC5.h
#pragma once
#include <string>
using namespace System;

namespace TestC5
{
public __gc class Class1
{
};

void method(void)
{
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);
}
}

I get this error :
TestC++5 error LNK2020: unresolved token (0A000006) _CxxThrowException
TestC++5 error LNK2020: unresolved token (0A000017) delete

If I substitute for #include <cstring>, I get :
TestC++5 error LNK2001: unresolved external symbol "char * __cdecl
strncpy(char *,char const *,unsigned int)"
(?strncpy@@$$J0YAPADPADPBDI@Z)
Am I missing another import library?

Alex.

Nov 17 '05 #5

P: n/a
Costea,

Thanks - so can you think of any reason why my project is not
compiling?

Do I need to use a different version of the runtime to the one that I
am given by default when creating a VS C++ Class Library (.NET)
project?

Alex.

"Costea Bogdan bcostea.ro>" <bogdan<at> wrote in message news:<42**********************************@microso ft.com>...
Nope, strncpy is in the runtime, not import libraries
--
cheers,
Costea Bogdan

Nov 17 '05 #6

P: n/a

"AlexD_UK" <al*********@hotmail.com> wrote in message
news:e3**************************@posting.google.c om...
When I try the first option with new Class Library (.NET) project and
the following code :

// TestC5.h
#pragma once
#include <string>
using namespace System;

namespace TestC5
{
public __gc class Class1
{
};

void method(void)
{
char str1[]= "To be or not to be";
char str2[6];
strncpy (str2,str1,5);
}
}

I get this error :
TestC++5 error LNK2020: unresolved token (0A000006) _CxxThrowException
TestC++5 error LNK2020: unresolved token (0A000017) delete


strncpy is part of the <cstring> header of the std namespace, not System
namespace which is part of the .net <mscorlib.dll>. Also, you need to make
sure you allocate enough n characters in strncpy(str2, str1, n) or it will
truncate. n needs to allocate enough room for the string plus the null
character at the end.

Just using standard c++, here's how I would rewrite and compile the code:

#include <cstring>
#include <iostream>
using namespace std;

int main()
{
const char str1[] = "To be or not to be";
char str2[24];
strncpy(str2,str1,23);

cout << str1 << endl;
cout << str2 << endl;
}

I do though, get this warning when compiling with VS.NET 8.0 beta:

cstrcpy.cpp(9) : warning C4996: 'strncpy' was declared deprecated
D:\Program Files\Microsoft Visual Studio 8\VC\include\string.h(121)
: see declaration of 'strncpy'
Microsoft (R) Incremental Linker Version 8.00.40607.16
Copyright (C) Microsoft Corporation. All rights reserved.

It is unsafe and cumbersome to use C style chars. Better to use safer
standard C++ strings:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str1 = "To be or not to be";
string str2 = str1;

cout << str1 << endl;
cout << str2 << endl;
}

str1 can be as long or short as you want, and you don't have to worry about
allocating enough when copying it to str2.

- Don Kim
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.