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

A

AlexD_UK

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.
 
J

Jacobo Rodriguez Villar

AlexD_UK said:
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 said:
Thanks,

Alex.


--
Jacobo Rodríguez Villar

TyphoonLabs Lead Programmer

http://www.typhoonlabs.com
 
C

Charles McDevitt

Jacobo Rodriguez Villar said:
--
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
 
A

AlexD_UK

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.
 
A

AlexD_UK

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.
 
D

Don Kim

AlexD_UK said:
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
 

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

Top