Thread: Hello World

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    16

    Unhappy 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++

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?)

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    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).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    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
    	}
    };

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to declare MyListener before you use it. A forward declaration would be fine.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    can you take the above example and modify how it should look?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    ahh, that works, great, thanks!

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    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();
    Last edited by Clonus; 10-07-2008 at 01:36 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    16

    Question

    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?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The 7 New Wonders of the World
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-08-2006, 01:55 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM
  5. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM