Thread: Passing a string to a constructor?

  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 10:44 PM.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    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
    28,413
    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.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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, 06:06 AM
  2. Passing array through class constructor
    By sonicdoomx in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 01: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, 07:31 PM