Jump to content
  • Advertisement
Sign in to follow this  
Toridas

having trouble passing strings in c++

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've decided to try to get into C++ programming. I messed around with it a little in the past but never as a OOP language. Now that I have experience with OOP in Java I decided to try my hand at C++ again. This is just a little test program I made to see if everything works, but so far it isn't. My problem starts when I start trying to pass a string as an argument.
//test.h
#ifndef TEST_H
#define TEST_H

//a very basic class definition
class Test
{
    public:
        Test();
        void hello();
        void integer(int number);
        void greet(string greeting);
};

#endif



//test.cpp
#include <iostream>
#include "test.h"

using namespace std;

//constructor
Test::Test()
{
    cout << "Hello ";
}

//test function 1, it works
void Test::hello()
{
    cout << "World\n";
}

//the next 2 functions look exactly the same
// except one uses int and one uses string
//yet the int one works and the string one does not. WHY?!
void Test::integer(int number)
{
    cout << number;
}

void Test::greet(string greeting)
{
    cout << greeting;
}

//test_app.cpp
#include &lt;iostream&gt;
#include "test.h"

using namespace std;

int main()
{
    cout &lt;&lt; "Testing!\n";
    Test bleh;                               //should output "Hello"
    bleh.hello();                            //should output "World"
    bleh.integer(123);                   //should output "123"
    string myGreeting = "GREET!";
    bleh.greet(myGreeting);           //should output "GREET!"
    
    //just adding this part so the window doesn't close 
    //right away when I run the program
    char asdf;
    cin &gt;&gt; asdf;
    
    return 0;
}



If I comment out all the parts about the greet function, I get no errors. The program outputs "Hello World" on one line and "123" on the next just like I expected. But if I leave the greet function in, I get this weird error from test.h that says "variable or field 'greet' declared void" and "expected ; before ( token."

Share this post


Link to post
Share on other sites
Advertisement
#include <string> and use the datatype ::std::string instead of just "string." There is no built-in string type in C++, it is a type defined in the C++ standard libraries. Edit: Haha I like how we both editted to say the same thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Toridas
hrm ok I'll try that. I thought strings would be included when using the std namespace


The std namespace just makes it so you would not need to type std::XXX. You are still responsible for including the proper header files. Like if you tried to use a vector now, you would get errors because <vector> is not included.

Share this post


Link to post
Share on other sites
D'oh. All I really needed was a using namespace std in test.h, or to do std::string isntead. No need to include <string>.

Thanks for the help

Share this post


Link to post
Share on other sites
That's because you had
#include <iostream>
#include "test.h"

if you had

#include "test.h"
#include <iostream>
you would need it to #include <iostream> in test.h. You want #include <iostream> in test.h because now it wont work if you use it in another file that doesn't include <iostream> or includes it in a different order. Remember that #include just copy and pastes the file where you put the include.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!