Thread: Passing a string to a constructor?

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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