Jump to content
  • Advertisement
Sign in to follow this  
musasabi

tokenizer function

This topic is 3798 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

so i needed a tokenizer function (C++). i found this - http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html - lovely little page and promptly stole theirs. however, when i compile it in my main.h (so every object has access to it if need-be), it acts like it cant declare string type variables. heres my implementation:
//main.h
#ifndef INC_MAIN_H
#define INC_MAIN_H

#include <iostream>
#include <algorithm>
#include <string>
#include <limits>
#include <list>
#include <vector>
#include <time.h>
#include <fstream>

enum COINAGE {PLATINUM, GOLD, SILVER, COPPER};
enum EXIT_STATUS {PLAYER_DIED, PLAYER_QUIT, PLAYER_WON};

int random(int lowerLimiter, int upperLimiter);

//http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
void tokenize(const string & str, vector <string> & tokens, const string & delimiters);

#endif



//main.c
#include "main.h"
#include "character.h"
#include "game.h"

#include "nav.h"

using namespace std;

int main(void)
{
	...
}

int random(int lowerLimiter, int upperLimiter)
{
	...
}

void tokenize(const string & str, vector <string> & tokens, const string & delimiters)
{
	// Skip delimiters at beginning.
	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	string::size_type pos     = str.find_first_of(delimiters, lastPos);

	while (string::npos != pos || string::npos != lastPos)
	{
		// Found a token, add it to the vector.
		tokens.push_back(str.substr(lastPos, pos - lastPos));
		// Skip delimiters.  Note the "not_of"
		lastPos = str.find_first_not_of(delimiters, pos);
		// Find next "non-delimiter"
		pos = str.find_first_of(delimiters, lastPos);
	}
}




and then the compiler output:
1>main.cpp
1>e:\projects\c++\rpg\rpg\main.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\projects\c++\rpg\rpg\main.h(19) : error C2143: syntax error : missing ',' before '&'
and so on for every cpp file that includes main.h. whats up?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
1>main.cpp
1>e:\projects\c++\rpg\rpg\main.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\projects\c++\rpg\rpg\main.h(19) : error C2143: syntax error : missing ',' before '&'


It's somewhat unproductive to list line numbers, then modify the source so they don't match up.

Especially when the error is in the very code you've removed.

Share this post


Link to post
Share on other sites
i didnt remove any code from that header. at all.

#ifndef INC_MAIN_H
#define INC_MAIN_H

#include <iostream>
#include <algorithm>
#include <string>
#include <limits>
#include <list>
#include <vector>
#include <time.h>
#include <fstream>

enum COINAGE {PLATINUM, GOLD, SILVER, COPPER};
enum EXIT_STATUS {PLAYER_DIED, PLAYER_QUIT, PLAYER_WON};

int random(int lowerLimiter, int upperLimiter);

//http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
void tokenize(const string & str, vector <string> & tokens, const string & delimiters);

#endif //main.h


thats the unmodified header. i removed one comment from the end and added the filename (as a comment) at the top for the forum post. =P

also, i said it was the tokenizer function's definition that was causing the error.

anyway. got any solutions?

Share this post


Link to post
Share on other sites
In your .h file, change all instances of 'string' to 'std::string' and 'vector' to 'std::vector'. Except, of course, in your include statements.

Share this post


Link to post
Share on other sites
Quote:
Original post by joshuanrobinson2002
You can do as Nairb suggests or you can add

using namespace std;

to your .h file as well.


It is strongly advised not to use "using" directives in header files, it leads to namespace pollution.

Share this post


Link to post
Share on other sites
Quote:
Original post by joshuanrobinson2002
You can do as Nairb suggests or you can add

using namespace std;

to your .h file as well.


But be warned that a fair amount of code you cut'n'paste from the internet wont work with this, because of name collisions.

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!