Passing a string to a constructor?

This is a discussion on Passing a string to a constructor? within the C++ Programming forums, part of the General Programming Boards category; Okay so I'm currently starting to work in SFML and I'm looking at making my first engine after studying a ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Passing a string to a constructor?

    Okay so I'm currently starting to work in SFML and I'm looking at making my first engine after studying a few other game engines I've seen. So basically right now I have a "gamestate" class then another class for levels. I'm trying to pass a string to the other class so I can change what the window title says to get an idea of how things work.. so I will show you an example.

    Code:
    #include <string>
    #include <iostream>
    Class A
    {
         public:
            void function(std::string);
    };
    void A::function(std::string Name)
    {
        std::cout<< Name;
    }
    
    int main()
    {
        A a;
        a.function("string to window");
    }
    I'm getting the following errors:
    Code:
    /home/cody/Documents/GameEngine/src/GameState.cpp||In member function ‘void GameState::Run()’:|
    /home/cody/Documents/GameEngine/src/GameState.cpp|18|error: no matching function for call to ‘Engine::Run(const char [7])’|
    /home/cody/Documents/GameEngine/include/Engine.h|20|note: candidates are: void Engine::Run()|
    ||=== Build finished: 1 errors, 0 warnings ===|
    I see that its trying to pass it is trying to pass it as a character array which obviously wont work, but how do i go about it.. do i need to typecast or something??

    thanks guys,

    Also.. not the appropriate forum but with things like image loading is it necessary to have a class?? I feel simply using functions would be easier. Thanks
    Last edited by codeblue; 04-26-2011 at 11:44 PM.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    then overload the constructor with one that take in character array.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Your example code does not demonstrate the error. (It has a different error due to Class versus class, but after fixing that it compiles perfectly fine.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Your example code does not demonstrate the error. (It has a different error due to Class versus class, but after fixing that it compiles perfectly fine.)
    Sorry for basically wasting your time laserlight. I forgot to put std::string in my prototype for the class. My mistake. I didn't event think to check it. Everything is up and working fine now though. Thanks for offering your time.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    When your car wont start, would you just send a drawing of your car engine to a mechanic?

    I.e. if you want the real problem fixed, show the real code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Your example code does not demonstrate the error. (It has a different error due to Class versus class, but after fixing that it compiles perfectly fine.)
    Sorry for basically wasting your time laserlight. I forgot to put std::string in my prototype for the class. My mistake. I didn't event think to check it. Everything is up and working fine now though. Thanks for offering your time.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    If you're passing an object to a function and it's not going to be changed, then make it const. Even better, avoid the copy and make it a const reference.

    Code:
    void A::function(const std::string &Name)
    {
        std::cout<< Name;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 07:06 AM
  2. Passing array through class constructor
    By sonicdoomx in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 02:42 PM
  3. At My Wit's End (concerns passing char to constructor)
    By MisterWonderful in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2004, 03:13 AM
  4. passing array to a constructor
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2003, 04:22 AM
  5. question about passing arg in constructor??
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2003, 08:31 PM

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