-
Hello World
Hi, I need to test some functionality between c++ and c#
I have this method
Code:
void SendOut(class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> > const &)
{
}
Can someone complete the code that would simply output the "string" passed into it?
I'm in very unfamilair waters with c++
-
Have you looked up "Hello world" in C++? That should give you some ideas.
Also, that is neither valid (class keyword not allowed in parameter lists, unnamed parameters technically allowed but a bad idea) nor a method (function not part of a larger class). Also, did you really try to build a string, not of characters, but of short ints? Why? (Or is that what the cool kids used these days instead of wchar_t?)
-
It seems that std::basic_string has overloads for cout only for some specializations (for character strings), but you can output the contents "char-by-char" using the copy algorithm.
BTW, you don't need the class and struct keywords in this context, and you might typedef the ugly type (also, the last two template arguments could be detected automatically from the first?):
Code:
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
/*
typedef
std::basic_string<unsigned short, std::char_traits<unsigned short>,std::allocator<unsigned short> >
ushort_string;
*/
typedef std::basic_string<unsigned short> ushort_string;
void SendOut(ushort_string const & s)
{
std::copy(s.begin(), s.end(), std::ostream_iterator<unsigned short>(std::cout, ""));
}
int main()
{
ushort_string s(10, 3);
SendOut(s);
}
-
hmm, I should have explained more, sorry.
I am trying to use a 3rd party DLL interop with C#, problem is, there is no marshal for basic_string, so I need to create some kind of wrapper for it. My first attempt would be to simulate the class in order to see what is happening.
The method from the DLL spells out as such (above).
-
While I'm explaining, perhaps you could help with this.
I am using Visual Studio 2008, I have the 3rd part DLL/LIB but I am not sure how to include this DLL (namespace) in the project?
I would usually add a reference to my project, however, c++ doesnt seem to have this feature. How do I include this object then use the namespace accordingly?
using namespace ThirdPartyObject
-
If you have a DLL, and they expect you to use it in C++, it should have a header file with it, which you would then #include. (If the functions are inside a namespace, you could do the using directive after that to save typing.) You would also need to make sure the linker knows about the DLL. (I have this vague recollection that it's #pragma import or something like that. You could search the forums since it's been asked before.)
-
This doesnt wanna compile, I'm not familair with "*" and what it's function is?
Can someone comment, or at least suggest how to fix this. This code below is a simplified version of an example I was given but doesnt work. "undeclared identifier"
Code:
class myclass
{
public:
void myclass::initclass()
{
MyListener* myListener;
myListener = new MyListener;
}
};
class MyListener: public ISomeInterface
{
public:
void
{
//do nothing
}
};
-
You have to declare MyListener before you use it. A forward declaration would be fine.
-
can you take the above example and modify how it should look?
-
You should probably lookup forward declarations if you're unclear on what they are. The simple solution is to add class MyListener; on a line above the myclass declaration. That might not work exactly for your real-world example, which is why it might be better for you to research that topic to understand better what is going on.
-
ahh, that works, great, thanks!
-
Ok, well it seems the template stuff maybe giving me a little more grief
I get this error when compiling
error LNK2001: unresolved external symbol "public: bool __thiscall NS::Client::initialize(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" (?initialize@Client@NS@@QAE_NABV?$basic_string@_WU ?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z)
It complies in a separte class but when I try to instatiate the class from a Console application I get the error above.
like so.
mySip::MySipApplication* xx1 = new mySip::MySipApplication();
xx1->InitSipApplication();
-
That's not a compile error. That's a linking error, which means everything is syntactically correct, but your program is in pieces. If you're in Visual Studio (which it looks like you are) then everything needs to be in one project (multiple files are fine, but one project) or you have to manually add the .obj files from the class into the current project.
-
hmmm, again im in unfamiliar territory. I am using VS2005
I have the includes and LIB file from the 3rd party API in a folder, which I have made "include"s from.
When you say everything needs to be in the same project, do you mean copy everything to a common folder?
-
No I mean in the project. If you have the .lib file, you don't have to put in the project; you merely need to add it to the linker commands (project properties, and then ... I forget where exactly. Poke around a little bit, you should see "additional libraries" or similar).