473,405 Members | 2,421 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,405 software developers and data experts.

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

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
6 4494
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

"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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Terepin | last post by:
Do I need to include some extra libraries when I use the STL <string> classes ? I've created a new Class Library (.NET) project, added a #include <string> directive to the main .cpp file, and...
2
by: Phil Ten | last post by:
Hello, I am working on a .NET C++ project based on the "Class Library (.NET)" wizard of Visual Studio .NET 2003. I would like to know if my .NET component is running in a ASP.NET page (.apsx...
5
by: Marco Zapletal | last post by:
hi group, i am facing the following problem: i have a vs.net (c#) project, which compiles into a class library. now i wrote a kind of testcase (with a main method()) which i want to execute...
4
by: Holly Li | last post by:
Hi, Because of reuse, I need to mix C# project and VB.net project. To illustrate the problem, I have built 3 simple projects: 1) C# library--projc (has one class "Manager") 2) vb.net...
2
by: Rafael Pivato | last post by:
Can I have two Class Libraries to share the same root namespace ? I want something like this: MySystem (root namespace) MyBO (class library A) MyDS (class library B) MyUS (class library C)
0
by: Phil | last post by:
I have created a Visual C++ Class Library Project and I am using the new and delete operators to allocate and release memory. But When I try to build the Project, I receive LNK2020 error from the...
3
by: Phaitour | last post by:
Hi there, I'm working on developing a large Class Library project that is slowly becoming a shared "framework" library amongst multiple applications. As this shared library grows, I need to...
4
by: Rainer Queck | last post by:
Hello NG, I started to build me a little class library. This library I added to a Project which also surves me to test the library. This library holds a namespace: namespace <myLib>.<some...
1
by: Fresno Bob | last post by:
Hi I am used to using the App_Code folder to put my DAL and BLL classes. However I want to move toward using separate dll's so I can start doing things like Unit Tests and using Sandcastle for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.