C++/CLI strings

Hello there, I've been wondering if something like typedef String^ string were possible. I've tried it, but it didn't work. Currently trying to put my current project in a windows forms application, win32api was a bit too complicated for now.

Attempt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// part of stdafx.h
#ifndef __STDAFX_H__
#define __STDAFX_H__

#include "StdAfx.h"

#include <windows.h>
#include <string>
//#ifdef string 
//#undef string
//#endif

using namespace std;

//typedef String^ string;
#endif 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// part of SYS.h
#ifndef __SYS_H__
#define __SYS_H__

#include "stdafx.h"

ref class SYS
{
public:
	SYS(void);

	void StringtoDouble(string &s, double &d);
};

#endif 


1>c:\users\rianne\documents\visual studio 2010\cli blend\SYS.h(11): error C2061: syntax error : identifier 'string'


EDIT: with the stuff uncommented I get these errors:
1>c:\users\rianne\documents\visual studio 2010\cli blend\SYS.h(11): error C2061: syntax error : identifier 'string'
1>c:\users\rianne\documents\visual studio 2010\cli blend\stdafx.h(27): error C2143: syntax error : missing ';' before '^'
1>c:\users\rianne\documents\visual studio 2010\cli blend\stdafx.h(27): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rianne\documents\visual studio 2010\cli blend\stdafx.h(27): error C2226: syntax error : unexpected type 'std::string'
Last edited on
Almost nobody here knows or uses C++/CLI. You should ask your question (and all future C++/CLI questions) in the MSDN Forums instead.
You cannot undef string because it's not #define'd

using namespace std; in a central header is a bad idea as well

as far as I know using STL within CLI/CLR isn't possible
@coder777
Yes you can use STL/unmanaged classes in CLI.
@Rii
If you want to use the name string for your typedef dont use the std namespace. Also void StringtoDouble(string &s, double &d); is probably an issue as String^ is a managed pointer(or reference, I forget) so you likely dont need the &. It also looks like you are including stdafx in stdafx?
Edit: Depending on where you put the typedef you may need to declare it as typedef System::String^ string;
Last edited on
@webJose: Thank you, I will do that next time ^^
@coder777: would it still be a bad idea to be using namespace std; in a central header of a c++ only project?
@naraku9333: using the std namespace while trying to define the CLI String^ as string was kinda dumb of me >_< could've thought of that ._."
I tried to typedef System::String^ string; sadly it still didn't work after removing using namespace std;, and also not after removing #include <string>
the #include "StdAfx.h" is different from the header "stdafx.h" somehow, it's a required include for every class(I don't know why, and I don't even see a file named like that in my project folder O_o), so I put it in there.
..And now after commenting out the include it somehow still compiles without error s: I have no idea why it was so persistent with including that file with the capital S and A.(I never had a file like that)

I guess this topic can be closed now, I'm gonna try something else in order to avoid merging these things into 1 project.
would it still be a bad idea to be using namespace std; in a central header of a c++ only project?
Yes, look at this discussion:

http://www.cplusplus.com/forum/general/72248/
@coder777: thank you! that discussion was very informative!
I'll be more careful with the namespaces from now on
Topic archived. No new replies allowed.