Hi guys! Thanks for moderating such an informative forum...this has become my best resource for c++ info!
I'm having what should be a familiar problem to any C++ classes newb. My questions revolve around the over all scoping of C++ classes. Maybe I've been at this for too long and have tried too many things and and am complicating things as usual.
I was hoping someone could think about my problem from the best practices standpoint... I'm so tired of being taught bad practices and have little confidence when it comes to how to go about doing something when there are SO many ways to do it!
What I'm trying to do is this: Write a tokenizer aka scanner program for a text file containing source data.
Basically I need to be able to suck in a text file and then use char/string manipulation (which I know I can do) so that the next successive token is returned when calling the scanner.
But I'm not even concerned with that part of it.
Before I program I try to get my structures working and my functions to just return. But I'm confused by the wealth of information available here and think im using concepts designed for different goals.
My Goals:
1- Have a main() that takes the program and then Initializes a Scanner() Class which initializes a TokenList() containing a vector of Token() objects.
2- Have scanner do the work, calling members of Tokenlist which fill the data from the Token Class.
I don't know if this makes any sense to anyone but I visualize it like this.
Main---Scanner::aScanner-----TokenList::tokenVector-----Token::attributes
My Problems/Questions:
1- does the over all template make sense?
2- Am I making this a lot harder than it is?
3- I've tried too many losing combinations to count.
4-What more do I have to do to be able to use a vector<Token> tokenVector?
5- I know I need friends, but do I need to "friend" things?
Please forgive me for the incredibly noob post and thank you guys in advance for any suggestions/advice links/snippits you can provide!
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <fstream>
#include <iostream>
#include "Scanner.h"
using namespace std;
int main ( int argc, char *argv[] ){
Scanner lizardScanner;
if ( argc != 2 )
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
ifstream the_file ( argv[1] );
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else {
cout<<"About to try creating a lizardScanner"<<endl;
lizardScanner = new Scanner();
cout<<"Returned to main after creating new lizardScanner"<<endl;
}
}
}
|
Scanner.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <fstream>
#include <iostream>
#include <cctype>
#include "TokenList.h"
class Scanner{
public:
Scanner();
private:
TokenList aTokenList;
};
|
Scanner.cpp
1 2 3 4 5 6 7 8 9 10 11 12
|
#include "Scanner.h"
Scanner::Scanner(){
cout <<"Default Scanner Constructor called"<<endl;
aTokenList = new TokenList();
cout << "Created aTokenList in Scanner.c"<<endl;
}
Scanner::Scan() Token{
}
|
TokenList.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <string>
#include <vector>
#include "Token.h"
//class Token; // Tell the compiler that Token is an internal class
class TokenList{
public:
TokenList(); //default constructor
int AddToken(std::string category);
int AddToken( std::string category, std::string attribute);
int RemoveLastToken();
// void ~TokenList();
private:
Token aToken;
vector<Token> tokenVector;
vector<Token>::iterator iter, iter2, beg, end;
}
|
TokenList.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include "TokenList.h"
TokenList::TokenList(){ //must be called once to init aToken
cout <<"Default TokenList Constructor Called"<<endl;
aToken = new Token();
tokenVector.clear();
iter = tokenVector.begin();
iter2 = tokenVector.begin();
beg = tokenVector.begin();
end = tokenVector.end();
cout << "Created aToken and tokenVector in TokenList.cpp"<<endl;
}
TokenList::AddToken(string category) int {
aToken = Token(category);
tokenVector.push_back(aToken);
iter++;
return tokenVector.size();
}
TokenList::AddToken(string category, string attribute) int {
aToken = Token(category, attribute);
tokenVector.push_back(aToken);
iter++;
return tokenVector.size();
}
TokenList::RemoveLastToken(){
tokenVector.erase(--iter);
return tokenVector.size();
}
//TokenList::~TokenList(){
// tokenList.~vector<Token>();
//}
|
Token.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <string>
class Token {
public:
Token();
Token(std::string category );
Token(std::string category, std::string attribute);
Token(const Token& a);
Token& operator=(const Token& a);
// ~Token();
private:
std::string tokenCategory;
std::string tokenAttribute;
};
|
Token.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include "Token.h"
Token::Token(){
cout<<"Default Token Constructor Called"<<endl;
tokenCategory = "defaultCategory";
tokenAttribute = "defaultAttribute";
cout<<"Initialized Token Default Values"<<endl;
}
Token::Token(string category){
tokenCategory = category;
tokenAttribute = "";
}
Token::Token(string category, string attribute){
tokenCategory = category;
tokenAttribute = attribute;
}
Token::Token(const Token& a){
Token anotherToken = new Token;
anotherToken->category = a->category;
anotherToken->attribute = a->category;
}
Token& Token::operator=(const Token& a){
if(this != &a){
delete anotherToken;
Token anotherToken = new Token;
anotherToken->category = a->category;
anotherToken->attribute = a->category;
}
return *this;
}
//Token::~Token(){}
|
If you've gotten this far then I already owe you.
ALL FEEDBACK IS GOOD FEEDBACK. Please don't tell me to give it up. I'm determined!
Respectfully,
-W