Jump to content
  • Advertisement
Sign in to follow this  
njpaul

Strange Error

This topic is 4725 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 created a dll for my physics engine and all is going well. I made a couple test programs and everything still works. Then I tried to put it into my game and all hell broke loose. I have a conversion operator inside my vector class to convert a vector into a string that looks like this:
inline operator std::string() const
{
	std::stringstream ss;
	ss << "CVector2(" << this->m_fX 
		<< "," << this->m_fY << ")";
	return ss.str();
}

I get an error saying that string is not a member of std. The thing is though, that it worked fine for every other program I did. I tried creating a new project to see if this error would surface again, since I had updated the dll since doing the other projects, and it still worked fine. It is just this one project that it refuses to work for. I think I might have to just start a new project and copy all the files from my old project into the new one to see if it works. Just doesn't make sense to me.

Share this post


Link to post
Share on other sites
Advertisement
To use std::string you have to include string before it. Perhaps your old projects included this header file before using the vector class, while the new project does not?

I mean, this would work:

#include <string>
#include <myvector.h>

whereas this would not:

#include <myvector.h>
#include <string>


It is about making the vector header file self-supporting, by including string in that file before using it. But then again this is just a wild guess. Greetz,

Illco

Share this post


Link to post
Share on other sites
Honestly that is horrible practice, complete abuse of conversion operators also you've force your self into a noke by using a std::string as its a type alias for std::basic_string a class template given char as the character type and std::char_traits<char> as the character traits type, and std::allocator<char> as the allocator type.

Instead overload stream insertion/extraction operators then you can use a string streams or other methods that use stream operators with your user-defined types to convert it to a string i.e.:


#include <istream>
#include <ostream>

struct vector2f {
float x, y;

vector2f(float x_ = 0.0f, float y_ = 0.0f)
: x(x_), y(y_) {}

};

template < typename CharT, typename Traits >
inline std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& in, const vector2f& v) {
return out << '(' << v.x << ',' << v.y << ')';
}

template < typename CharT, typename Traits >
inline std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, vector2f& x) {
Tp nx, ny;
CharT ch;

is >> ch;

if(ch == '(') {
is >> nx >> ch;
if(ch == ',') {
is >> ny >> ch;
if(ch == ')')
x = vector2f(nx, ny);
else
is.setstate(std::ios_base::failbit);
} else
is.setstate(std::ios_base::failbit);
} else
is.setstate(std::ios_base::failbit);
return is;
}


sample:


#include <sstream>
#include <string>

int main() {

vector2f v(30.0f, 40.0f);

std::ostringstream oss;

oss << "my vector: " << v << std::endl;

std::string s = oss.str();

}

Share this post


Link to post
Share on other sites
I already overloaded the insertion and extraction operators, and all the appropriate header files are included. I just wanted a way I could cast it into a string so that I can print it out with my debugging functions, which take string arguments. I suppose I could change it to a ToString function instead of a casting, if that is better practice. Can you explain why it is abuse of the conversion operator for me?

Share this post


Link to post
Share on other sites
Quote:
Original post by njpaul
I already overloaded the insertion and extraction operators, and all the appropriate header files are included. I just wanted a way I could cast it into a string so that I can print it out with my debugging functions, which take string arguments. I suppose I could change it to a ToString function instead of a casting, if that is better practice. Can you explain why it is abuse of the conversion operator for me?


Don't change it to a ToString function, just follow snk_kid's advice instead.

Share this post


Link to post
Share on other sites
I think I found the problem. My Physics dll has a file called Globals.h and .cpp and so did my main project. I think the compiler was getting confused as to which one to use. I kind of suspected this last night, but didn't try it out until now. The main project compiles fine now.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!