So I am trying to compile my program and it tells this: error: class std:: queue
ID: 3734542 • Letter: S
Question
So I am trying to compile my program and it tells this:
error: class std:: queue < std vector basic_string <char> >> has no members named Tokenize, GetTokens. What am I doing wrong?
those two functions are on my other class and they are public and I did include the header file on both this driver and my other class
this driver reads in a .txt file and sends it to my tokenize function which accepts a string parameter tokenizes it and GetTokens is a vector of strings which gets the tokenized tokens
#include "prog2_1.hpp"//header file
#include <iostream>
#include <queue>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]){
int j = 0;
ifstream file(argv[1]);
while(!file.eof()){
string a;
getline(file,a);
queue<vector<string> > token;
token.Tokenize(a);//error
cout << "error on line "<< j << ": ";
while(!token.GetTokens.empty()){error
cout << token.GetTokens() << " ";
}
j++;
}
}
#include "prog2_1.hpp"//header file
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
Tokenizer::Tokenizer(){
this->data = new queue<vector<string> >();
}
Tokenizer::~Tokenizer(){
delete this->data;
}
void Tokenizer::Tokenize(string input){
stringstream oss(input);
vector<string> line;
while(!oss.eof()){
string s;
oss >> s;
if(line.size() == 2 && line.at(0) == "push")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "pop")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "add")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "sub")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "mul")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "div")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "mod")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "skip")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "save")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "get")
line.push_back(s);
else
throw ("Unexpected token: " + s);
}
this->data->push(line);
}
vector<string> Tokenizer:: GetTokens(){
vector<string> out;
while(!this->data->empty()){
out = this->data->front();
this->data->pop();
}
if(this->data->empty())
throw "No tokens";
return out;
}
Explanation / Answer
Actually Tokenize() and GetTokens() Functions are Defined in Second class(header2.hpp) header file.
and Declared in one header file.and Calling from the Driver/Main Function.
So if you want Use the these two Functions u Need to include both Header files like prog2_1.hpp and header2.hpp
For Example,
Main Program:-
--------------------
main.cpp
#include "prog2_1.hpp"//header file
#include "header2.hpp"/------you have include the header in main function.---------------
#include <iostream>
#include <queue>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int j = 0;
ifstream file(argv[1]);
while(!file.eof())
{
string a;
getline(file,a);
queue<vector<string> > token;
token.Tokenize(a);//error
cout << "error on line "<< j << ": ";
while(!token.GetTokens.empty())
{
error
cout << token.GetTokens() << " ";
}
j++;
}
}
header2.hpp:-
----------------
#include "prog2_1.hpp"//header file
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
Tokenizer::Tokenizer()
{
this->data = new queue<vector<string> >();
}
Tokenizer::~Tokenizer()
{
delete this->data;
}
void Tokenizer::Tokenize(string input)
{
stringstream oss(input);
vector<string> line;
while(!oss.eof())
{
string s;
oss >> s;
if(line.size() == 2 && line.at(0) == "push")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "pop")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "add")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "sub")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "mul")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "div")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "mod")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "skip")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "save")
line.push_back(s);
else if(line.size() == 2 && line.at(0) == "get")
line.push_back(s);
else
throw ("Unexpected token: " + s);
}
this->data->push(line);
}
vector<string> Tokenizer:: GetTokens()
{
vector<string> out;
while(!this->data->empty())
{
out = this->data->front();
this->data->pop();
}
if(this->data->empty())
throw "No tokens";
return out;
}
still If you have any Doubts put a comment.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.