Sign in to follow this  
musasabi

tokenizer function

Recommended Posts

musasabi    133
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
Antheus    2409
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
musasabi    133
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
Nairb    766
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
rip-off    10976
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this