A case od the most vexing parse - c++

My code keeps giving me error Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream<char>') and 'const bus') Is this a case of the most vexing parse? if so how to fix it. I am trying to print out objects stored in vector. std::vector<bus> v = {} is my vector to contain the object whereas bus is my class
#include <iostream>
#include <vector>
class bus{
public:
int carNum, releaseYear;
};
int temp1, temp2;
void print(std::vector<bus> const &input)
{
for (auto it = input.cbegin(); it != input.cend(); it++)
{
std::cout << *it << ' '<< std::endl;
}
}
int main()
{
bus bus1;
bus1.carNum = 0;
bus1.releaseYear = 0;
bus bus2;
bus2.carNum = 0;
bus2.releaseYear = 0;
// Create a vector containing objects
std::vector<bus> v = {};
// Add two more integers to vector
std::cout<<"enter number"<<std::endl;
std::cin>>temp1;
temp1 = bus1.carNum;
std::cout<<"enter year"<<std::endl;
std::cin>>temp2;
temp2 = bus1.releaseYear;
v.push_back(bus1);
print(v)
}

There is no vexing parse here. You simply need to overload the stream insertion operator for the bus type. Since bus only has public data members, this can be a non-friend function:
std::ostream& operator<<(std::ostream& stream, bus const& b)
{
stream << "#" << b.carNum << " Year" << b.releaseYear;
return stream;
}
You can of course, format your output however you want.

Related

How to use pointer with struct to refer to the field of each struct

Below code is the normal way to get the input from a text and store it in an array in a structure.
Wanted to ask how can i use pointer to store all these data into the array of structure ? Like p1->Years (this is without array, but how can i apply this to way of writing in below code)
Any better suggestion to use pointer to take in the input?
int years = 4;
struct maju_company {
int Year;
float quarter1, quarter2, quarter3, quarter4, total_sales, average_sales;
};
int main() {
string line;
maju_company p1[years];
fstream yeecinnfile("MajuSales.txt");
if(yeecinnfile.is_open()) {
//ignoring the first four line of code and store the rest of the code
string line1,line2,line3,line4;
getline(yeecinnfile,line1);
getline(yeecinnfile,line2);
getline(yeecinnfile,line3);
getline(yeecinnfile,line4);
while(!yeecinnfile.eof()) {
for(int i =0; i<years; i++) {
yeecinnfile>>p1[i].Year>>p1[i].quarter1>>p1[i].quarter2>>p1[i].quarter3>>p1[i].quarter4;
}
}
for(int i =0; i<years; i++) {
cout<<p1[i].Year<<setw(10)<<p1[i].quarter1<<setw(10)<<p1[i].quarter2<<setw(10)<<p1[i].quarter3<<setw(10)<<p1[i].quarter4<<endl;
}
cout<<endl;
}
}
I see nothing wrong with the way you do this.
However, you could create a pointer to each record inside the loop
maju_company* p = &p1[i];
and then use p-> instead of p1[i]., but I really don't see this as an improvement.
If the reading loop looks too complicated, I would rather move the code to a separate function, perhaps
void read_record(maju_company& company);
or maybe
maju_company read_record();
and then only have to handle a single company inside the function (so no indexing and no ponters there).
I think you wouldn't need pointers at all for your example.
Use a std::vector to hold all your data and then there are other
things from C++ I think you should learn to use, example here :
(if you have questions let me know)
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
// dont use : using namespace std;
struct maju_company_data
{
int year;
float quarter1, quarter2, quarter3, quarter4, total_sales, average_sales;
};
// describe how to stream data to an output stream (like std::cout)
std::ostream& operator<<(std::ostream& os, const maju_company_data& data)
{
os << "-----------------------------------------------------\n";
os << "Company data for year : " << data.year << "\n";
os << "Quarter 1 : " << data.quarter1 << "\n";
os << "Quarter 2 : " << data.quarter1 << "\n";
os << "Quarter 3 : " << data.quarter1 << "\n";
os << "Quarter 4 : " << data.quarter1 << "\n";
os << "\n";
return os;
}
int main()
{
// no need to manage pointers yourself use a vector
std::vector<maju_company_data> company_yearly_data; // give variables a meaningful name
std::ifstream ifile("MajuSales.txt"); // ifstream your using file as input
std::string line1, line2, line3, line4;
// ignore first line
ifile >> line1;
while (ifile >> line1 >> line2 >> line3 >> line4) // probably you need to read a few more lines here
{
maju_company_data data;
// convert read strings to numbers
data.year = std::stoi(line1);
data.quarter1 = std::stof(line2);
data.quarter2 = std::stof(line3);
data.quarter3 = std::stof(line4);
//..
//data.quarter4 = std::stof(line5);
//data.total_sales = std::stof(line6);
company_yearly_data.push_back(data);
};
// this is a range based for loop
// it is prefered since you cant go out of bounds
// const auto& means that data will be an unmodifiable
// reference to each of the structs stored in the vector
for (const auto& data : company_yearly_data)
{
std::cout << data; // since we overloaded << this loop will be nice and clean
}
return 0;
}
A C++ approach to this to overload the istream operator>> and ostream operator<< for your specific type. E.g.
#include <algorithm>
#include <array>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
static constexpr auto years{4};
struct maju_company {
int Year{};
float quarter1{}, quarter2{}, quarter3{}, quarter4{};
float total_sales{}, average_sales{}; // ALWAYS init your floats.
};
auto& operator>>(std::istream& is, maju_company& mc) {
is >> mc.Year
>> mc.quarter1 >> mc.quarter2 >> mc.quarter3 >> mc.quarter4
>> mc.total_sales >> mc.average_sales;
return is;
}
auto& operator<<(std::ostream& os, maju_company const& mc) {
os << mc.Year
<< std::setw(10) << mc.quarter1
<< std::setw(10) << mc.quarter2
<< std::setw(10) << mc.quarter3
<< std::setw(10) << mc.quarter4;
return os;
}
You can then go on to use the type using the std library, e.g.
int main() {
auto p1{std::array<maju_company, years>{}};
{
auto fs{std::fstream("MajuSales.txt")};
if (!fs.is_open()) return -1;
{
// throw away 4 lines
auto dummy{std::string{}};
for (auto i{0}; i < 4; ++i) getline(fs, dummy);
}
std::copy_n(std::istream_iterator<maju_company>{fs},
years,
begin(p1));
}
std::copy(cbegin(p1), cend(p1),
std::ostream_iterator<maju_company>{std::cout, "\n"});
}

How to convert from std::vector<float> to std::istream?

I have an std::vector and the function expects an std::istream:
callMe(std::istream& is)
What is the best way to do the conversion? Is there something more clever than?
std::stringstream sstr;
for(int i = 0; i < myVector.size(); ++i) {
sstr << myVector[i] << " ";
}
std::istringstream istr{sstr.str()};
callMe(istr);
EDIT: Thanks for the suggestions so far! Updated code:
std::stringstream sstr;
for(const float& val : myVector) {
sstr << val << " ";
}
callMe(sstr);
The issue is that std::istream is inherently character-based. If you want to keep using callMe(std::istream& is) as an interface, you are bound to convert every element of myVector to characters and back at some point. If you want to stick with this option, I personally find ostream_iterator an elegant solution:
copy(begin(data), end(data), std::ostream_iterator<float>(sstr));
Full example:
void callMeStream(std::istream &is)
{
float f1;
is >> f1;
std::cout << "Stream: " << f1 << std::endl;
}
// ...
std::vector<float> data = {3.5, 1.5, 2.5 /* ... */};
std::stringstream sstr;
copy(begin(data), end(data), std::ostream_iterator<float>(sstr));
callMeStream(sstr); // Output: "Stream: 3.51"
If you are willing to change the signature of callMe, this conversion can be avoided:
template <class T>
void callMeTemplate(T &is)
{
float f1;
is >> f1;
std::cout << "Template: " << f1 << std::endl;
}
#define NO_ELEMENT -1.0;
class data_wrapper
{
std::vector<float>::const_iterator current;
const std::vector<float>::const_iterator last;
public:
data_wrapper(const std::vector<float> &data) : current(begin(data)), last(end(data)) {}
data_wrapper &operator>>(float &value)
{
if (last == current)
{
value = NO_ELEMENT;
}
else
{
value = *current++;
}
return *this;
}
};
// ...
data_wrapper wrapper(data);
callMeTemplate(wrapper); // Output: "Template: 3.5"
In the second example, the float value never gets converted to a character sequence, and could accept both data_wrapper and std::istream types. Of course, if you are willing to change the signature of callMe entirely, you might as well change it to accept a begin/end iterator range or a vector directly.

Array based on strings, char list conversions

I create a multiple table of string type. I keep variables inside (int, string). It gives me an error:
[Error] cannot convert 'std::string {aka std::basic_string}' to 'char' in assignment
I've created a tree-shaped suite of functions.The program create a multiple array from a file with this format:
11 10 2001
CSKA Moscow
12 1
Bayern Munich
...
Program:
void llegir(std::fstream &_contingut, std::string *_taula) {
//declaro variables
int dia, mes, any, puntsLocal, puntsVisitant, i = 0;
std::string equipLocal, equipVisitant;
while (!(_contingut.eof())) {
//llegeixo arxiu
_contingut >> dia >> mes >> any; //primera linea
_contingut.ignore();
getline(_contingut, equipLocal); //segona linea
_contingut >> puntsLocal >> puntsVisitant; //tercera linea
_contingut.ignore();
getline(_contingut, equipVisitant); //quarta linea
_taula[i][0] = dia;
_taula[i][1] = mes;
_taula[i][2] = any;
_taula[i][3] = equipLocal.c_str();
_taula[i][4] = puntsLocal;
_taula[i][5] = equipVisitant.c_str();
_taula[i][6] = puntsVisitant;
i++;
}
}
void creartaulaDelFitxer(std::string _fitxer, std::string *_taula, int &n_taula) {
std::fstream arxiu;
arxiu.open(_fitxer, std::fstream:: in );
if (arxiu.is_open()) {
std::cout << "existeix";
} else {
std::cout << "ERROR OBERTURA FITXER";
}
llegir(arxiu, _taula);
}
int main(int argc, char** argv) {
std::string fitxer;
std::string eurolliga[300][7];
int n_taula = 0;
std::cout << "INTRODUEIX NOM FITXER:" << std::endl;
std::cin >> fitxer;
creartaulaDelFitxer(fitxer, *eurolliga, int n_taula);
}
You are mixing pointers, chars and strings which will certainly cause a lot of headache. Try to use the standard containers, like std::string and std::vector. If you need many strings, put them in a vector. When you have a collection of data like
11 10 2001
CSKA Moscow
12 1
Bayern Munich
that describes some entity, create a class for it. You can then add streaming operators for that class to read in one of these entities. If you have a collection of entities, make a container and add streaming operators for that too.
Example:
#include <iostream>
#include <fstream>
#include <vector>
class Game {
std::string equipLocal{};
std::string equipVisitant{};
int dia{}, mes{}, any{};
int puntsLocal{}, puntsVisitant{};
public:
friend std::istream& operator>>(std::istream&, Game&);
friend std::ostream& operator<<(std::ostream&, const Game&);
};
// read one entity from an istream
std::istream& operator>>(std::istream& is, Game& g) {
if(is >> g.dia >> g.mes >> g.any) {
is.ignore();
if(std::getline(is, g.equipLocal) && (is >> g.puntsLocal >> g.puntsVisitant)) {
is.ignore();
std::getline(is, g.equipVisitant);
}
}
return is;
}
// write one entity to an ostream
std::ostream& operator<<(std::ostream& os, const Game& g) {
return os << g.dia << " " << g.mes << " " << g.any << "\n"
<< g.equipLocal << "\n"
<< g.puntsLocal << " " << g.puntsVisitant << "\n"
<< g.equipVisitant << "\n";
}
class EuroLiga {
std::vector<Game> games{};
public:
bool Load(const std::string& filename) {
std::ifstream arxiu(filename);
if(arxiu) {
games.clear();
arxiu >> *this; // use this class' friend, operator>>
return true;
} else
return false;
}
// support for basic non-const iteration over the 'games'
std::vector<Game>::iterator begin() { return games.begin(); }
std::vector<Game>::iterator end() { return games.end(); }
friend std::istream& operator>>(std::istream&, EuroLiga&);
};
// read all entities from an istream
std::istream& operator>>(std::istream& is, EuroLiga& el) {
Game tmp;
while(is >> tmp) {
el.games.push_back(std::move(tmp));
}
return is;
}
int main() {
EuroLiga euroliga;
std::string fitxer;
std::cout << "INTRODUEIX NOM FITXER: ";
std::cin >> fitxer;
euroliga.Load(fitxer);
// display all entities read from the file
for(auto& g : euroliga) {
std::cout << g << "\n";
}
}
void llegir(std::fstream &_contingut, std::string *_taula)
Gets a pointer to a string called _taula, this is probably your array.
However you assign something to your array like this:
_taula[i][0] = dia; // allowed, but bad because char is basically a number.
[...]
_taula[i][3] = equipLocal.c_str(); // not allowed, you are assigning a char pointer to a char.
taula[i] is the i-th string in your array. And by putting [0] you assign to the first character in that string. dia is an integer though.
For example
std::string[] = {"Hello", "world", "I", "am", "alive"};
std::cout << string[1] << std::endl; // output "world"
std::cout << string[1][0] << std::endl; // output 'w'
You can not assign a string to a single character.
As a side note, you should look into declaring an enumeration for your array index (and a constant for it's size) to make it more clear and improve maintainability.
What you should probably be doing is create a struct or class for your, whatever it is
struct whateverItIs {
int dia, mes, any, puntsLocal, puntsVisitant;
std::string equipLocal, equipVisitant;
};
Make a new instance of that in your llegir and push it to the back of a std::vector you get by reference.
Just remember to delete() them later especially before that vector goes out of scope.

Setting a variable in a child class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am currently working on a very basic assembler. The assembler needs to take in assembly instructions and output 16-bit binary instructions for use with a computer we are making.
My design strategy has been to create a Command class, that has 3 child classes. There is one for each type of command: A-commands, C-commands, and L-commands. To identify the type of command I am working with, I have included a string command_type that is either "A", "C", or "L" respectively.
EDIT:
I am still having a lot of trouble figuring out how to properly derive these classes. Basically, A and L commands should have a "symbol" string, which represents an integer value that needs to be converted, while C commands have "dest","comp", and "jump" values that also must be accessed, however they do not have "symbol" values.
Command.h
#include <fstream>
#include <string>
class Command {
std::string command_type = "";
protected:
void set_commandType(std::string x){command_type = x;}
public:
Command();
virtual ~Command();
std::string commandType() const {return command_type;}
};
class A_COMMAND : public Command
{
std::string symbol;
public:
A_COMMAND(std::string s);
std::string get_symbol(){return symbol;}; //Returns the symbol or decimal Xxx of the current command #Xxx or (Xxx) . Should be called only when commandType() is A_COMMAND or L_COMMAND.
};
class C_COMMAND : public Command
{
std::string comp;
std::string dest;
std::string jump;
public:
C_COMMAND(std::string s, std::string d, std::string j);
std::string get_comp(){return comp;}; //Returns the comp mnemonic in the current C-command (28 possibilities). Should be called only when commandType() is C_COMMAND.
std::string get_dest(){return dest;}; //Returns the dest mnemonic in the current C-command (8 possibilities). Should be called only when commandType() is C_COMMAND.
std::string get_jump(){return jump;}; //Returns the jump mnemonic in the current C-command (8 possibilities). Should be called only when commandType() is C_COMMAND.
};
class L_COMMAND : public Command
{
std::string symbol;
public:
L_COMMAND(std::string s);
std::string get_symbol(){return symbol;}; //Returns the symbol or decimal Xxx of the current command #Xxx or (Xxx) . Should be called only when commandType() is A_COMMAND or L_COMMAND.
};
Command.cpp
#include "Command.h"
//---------------------------------------------
//A-Command functions
Command::Command(){}
A_COMMAND::A_COMMAND(std::string s) : symbol(s)
{
set_commandType("A");
}
//---------------------------------------------
//C-Command functions
C_COMMAND::C_COMMAND(std::string c, std::string d, std::string j) : comp(c), dest(d), jump(j)
{
set_commandType("C");
}
//---------------------------------------------
//L-Command functions
L_COMMAND::L_COMMAND(std::string s) : symbol(s)
{
set_commandType("L");
}
I have a Parser.cpp and Parser.h that process the input and are responsible for creating a deque of commands:
Parser.h
#include "Command.h"
#include <vector>
#include <deque>
class Parser {
private:
std::deque<Command> commands;
public:
Parser(std::vector<std::string>);
bool hasMoreCommands() //are there more commands in the input?
{
if(commands.size() != 0)
return true;
else
return false;
}
void advance(){commands.pop_front();} //move to next command, should only work if hasMoreCommands returns false}
Command currentCommand(){return commands.front();}
std::vector<std::string> translateCommands(); //convert commands into binary strings
};
Parser.cpp
#include "Parser.h"
#include "Command.h"
#include <vector>
#include <iostream>
#include <string>
#include <unordered_map>
bool inList(std::string& str, std::vector<std::string> list) //check if a given string contains one of the elements in the comp, dest, jump vectors. if so extract string for use in constructor
{
for(auto i = list.begin(); i!=list.end(); ++i)
{
std::size_t found = str.find(*i);
if(found!=std::string::npos)
{
return true;
}
}
return false;
}
Parser::Parser(std::vector<std::string> input) {
std::vector<std::string> dest_list = {"","M","D","MD","A","AM","AD","AMD"}; //all possible dests
std::vector<std::string> comp_list = {"0","1","D","A","!D","!A","-D","-A","D+1","A+1","D-1","A-1","D+A","D-A","A-D","D&A","D|A","M","!M","-M","M+1","M-1","D+M","D-M","M-D","D&M","D|M"}; //all possible comps
std::vector<std::string> jump_list = {"","JGT","JEQ","JGE","JLT","JNE","JLE","JMP"}; //all possible jumps
std::string dest, comp, jump;
std::deque<Command> commands;
for(std::vector<std::string>::const_iterator i = input.begin(); i != input.end(); ++i)
{
std::string line = *i;
if(*line.begin()=='#') //A-command
{
A_COMMAND command(line.substr(1));
std::cout << "Command type: " << command.commandType() << "\n";
std::cout << "symbol: " << command.get_symbol() << "\n";
commands.push_back(command);
}
else if(*line.begin()=='(' && *line.rbegin() == ')' && line.size() > 2) //L-command
{
L_COMMAND command(line.substr(1, line.size() - 2));
std::cout << "Command type: " << command.commandType() << "\n";
std::cout << "symbol: " << command.get_symbol() << "\n";
commands.push_back(command); }
else
{
std::string rhs = line;
std::string dest_string = "";
std::string comp_string = "";
std::string jump_string = "";
size_t equals_pos = line.find('='); //position of = in string, if present
size_t semi_pos = line.find(';'); //position of ; in string, if present
if(equals_pos != line.npos) //if there is an = then we have a dest
{
dest_string = line.substr(0,equals_pos);
rhs = line.substr(equals_pos+1);
}
if(semi_pos != line.npos) //jump
{
comp_string = rhs.substr(0,semi_pos);
jump_string = rhs.substr(semi_pos+1);
}
else //no jump
{
comp_string = rhs;
}
//now confirm if inputs are valid
if(inList(dest_string, dest_list))
dest = dest_string;
else
std::cout << "invalid dest \n";
if(inList(comp_string, comp_list))
comp = comp_string;
else
std::cout << "invalid comp \n";
if(inList(jump_string, jump_list))
jump = jump_string;
else
std::cout << "invalid jump \n";
C_COMMAND command(comp, dest, jump);
std::cout << "Command type: " << command.commandType() << "\n";
std::cout << "dest: " << command.get_dest() << "\n";
std::cout << "comp: " << command.get_comp() << "\n";
std::cout << "jump: " << command.get_jump() << "\n";
commands.push_back(command);
}
}
}
My main.cpp loads the input, and passes it through the parser. The problem I have is that I cannot do anything with the input.
I have tried to write a function like so:
string translateLine(Command command, Code code) //Code is a table for translating the command
{
string output;
if(command.commandType() == "A")
{
string symbol = parser.currentCommand().get_symbol();
cout << symbol << endl;
//perform binary conversion
}
/*else if(command.commandType() == "C")
{
string dest = command.get_dest();
}*/
//shouldn't be any L commands in symbol-less version
else
{
std::cout << "unexpected command value \n";
}
return output;
}
But as soon as I call get_symbol(), the compiler doesn't recognize the function. I know that this is because the base Command doesn't have a get_symbol() function, but I can't figure out how to correctly add the functions to the base class and derive them to the lower 3. I can't just make the pure virtual because not all of the functions are used in each class. How can I correctly accomplish this?
First, if translateLine() should be able to accept A_COMMAND, C_COMMAND, or L_COMMAND objects, then it needs to take a Command* parameter, not a Command parameter. A pointer to a base class can hold a pointer to a class derived from that base, but an object of the base class cannot hold a derived object.
Second, you cannot call a function that belongs to A_COMMAND even with a Command pointer that is really pointing to a A_COMMAND object without doing a dynamic_cast. A dynamic_cast can convert a pointer from Command* to A_COMMAND at run-time and will return NULL if the object pointed to is not really an A_COMMAND object.
The basic problem is that you're reinventing Run-Time Type information. You don't need to add "command_type=A" to class A_COMMAND. C++ already knows what types your objects have, if there's at least one virtual method. And it looks like your class needs a virtual destructor.

Link List - no operator '<<' matches

My c++ is really poor. Anyhow with the code snippet bellow why do I get a error on the << in the do while loop when outside of it I get no error. The error is: no operator "<<" matches these operands. However the string w picks up the word fine. I read somewhere I may have to overload it but why? And how would I over load it for a link list.
Thanks in advance.
void print()
{
HashTable *marker = headOne;
HashTable *inList;
for( int i = 0; i < tableSize; i++ )
{
cout << i << ": " << marker->number << endl;
if(marker->child != NULL)
{
inList = marker;
do
{
string w = inList->word;
cout << w << endl;
inList = inList->child;
}
while(inList != NULL);
}
marker = marker->next;
}//end for loop
}
In order to be able to cout a std::string you have to include:
#include <string>
#include <iostream>
This works:
// Missing includes and using
#include <string>
#include <iostream>
using namespace std;
// missing struct
struct HashTable {
HashTable* next;
HashTable* child;
string word;
int number;
};
// missing vars
HashTable ht;
HashTable* headOne = &ht;
int tableSize = 5;
// Unchanged
void print()
{
HashTable *marker = headOne;
HashTable *inList;
for( int i = 0; i < tableSize; i++ )
{
cout << i << ": " << marker -> number << endl;
if(marker->child != NULL)
{
inList = marker;
do
{
string w = inList -> word;
cout << w << endl;
inList = inList -> child;
}
while(inList != NULL);
}
marker = marker -> next;
}//end for loop
}
I read somewhere I may have to overload it but why?
Because there's no overload that matches your need.
And how would I over load it for a link list.
You can do this outside of your class or struct:
(where T is the type of the object that you want to print)
std::ostream& operator<<(std::ostream& os, const T& obj)
{
/* write obj to stream */
return os;
}
This is just an example that prints a vector:
std::ostream& operator<<(std::ostream& os, vector<int>& obj)
{
for (auto &i : obj)
os << i << " ";
return os;
}
Then I would be able to simply do this cout << n.vec; where n is the class object and vec the name of a vector of ints.