New to CPP -> convert Object to String

This is a discussion on New to CPP -> convert Object to String within the C++ Programming forums, part of the General Programming Boards category; I'm confused by the pointers.. Code: RegistryKey * pRegKey = Registry::LocalMachine; pRegKey = pRegKey->OpenSubKey(L"SOFTWARE\MySoft"); Object *pValue = pRegKey->GetValue(L"InstallDir"); Console::WriteLine(L"Directory: {0}", ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    New to CPP -> convert Object to String

    I'm confused by the pointers..

    Code:
    RegistryKey * pRegKey = Registry::LocalMachine;
    pRegKey = pRegKey->OpenSubKey(L"SOFTWARE\\MySoft");
    Object *pValue = pRegKey->GetValue(L"InstallDir");
    Console::WriteLine(L"Directory: {0}", pValue);
    Ok, so I need to convert pValue to a string or char*.. I can't figure out how to do that..

    I tried
    Code:
    std::cout(pValue->ToString);
    But i get this error:

    error C2064: term does not evaluate to a function

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    "cout" isn't a function, so your last statement is incorrect. I also have no idea what the "Object" object is refering to, perhaps you can post what libraries you're using or the code for the "Object" class if you wrote it yourself.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    Yes, I messed up, srd::cout(blah) is wrong..

    Code:
     std::cout << pValue->ToString;
    This just displays 1..

    No I don't have any class named Object.. pRegKey->GetValue(L"InstallDir"); This returns a type-Object.. Hence:

    Code:
    Object *pValue = pRegKey->GetValue(L"InstallDir");
    .. But i don't understand why *pValue (pointer) instead of just pValue..

    This is all I'm including:

    Code:
    #using <mscorlib.dll>
    using namespace System;
    using namespace Microsoft::Win32;
    
    #include <string>
    #include <iostream>
    #include <stdio.h>
    #include <conio.h>

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by smash84
    Code:
    #using <mscorlib.dll>
    using namespace System;
    using namespace Microsoft::Win32;
    You may get better answers in the Windows Programming forum. Don't repost, though, a moderator will move it if they feel it to be nessassary.
    Sent from my iPadŽ

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    #include <stdio.h>
    In C++ the standard C libraries have been renamed by dropping the trailing .h and adding a leading 'c', so the above include should be
    Code:
    #include <cstdio>
    If your compiler doesn't have <cstdio>, get Dev-C++.

    Perhaps google can answer your question: http://www.google.ca/search?hl=en&q=...o+String&meta=
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by smash84
    Code:
    #using <mscorlib.dll>
    using namespace System;
    using namespace Microsoft::Win32;
    you are using either managed C++ or C++/CLI. This is the C++ binding to .net and as such, non-standard.

    anyway ToString is a method. you need to call it like so
    Code:
     std::cout << pValue->ToString();
    but I'm not even sure that will work as ToString returns a .Net String object instead of a std::string.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    This compiles, but still only prints 1:

    Code:
    std::cout << pValue->ToString();
    I've also changed include to: #include <cstdio> instead of #include <stdio.h>

    Also, why won't this compile:

    Code:
    String hello = pValue->ToString();
    ?

    'hello' : 'System::String' differs in levels of indirection from 'char [50]'

    CONFUSING!!!

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
    pValue->ToString();
    Those error probably mean that the return value of the above expression is in the .NET's own type, which is incompatible with std:: C++ classes and functions. Looks like you'll have to go .NET all the way (which is probably what they wanted you to do in the first place).
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    #include <iostream>
    #include <stdio.h>
    these are supplying the same functions, the c and the c++ versions. you don't need both in any application, unless to want conflicts to crash it.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    No nothing, I still get '1' instead of the string.. Can't figure out how to solve this problem..

    Anyone know a better way to access registry key values?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21