Too many arguments in function call. c++ - c++

So my goal is to pass this vector vector 'Beer' allBeers to a function UnitedStatesBeer::getBeerTop(), but when I try to do that, I get the error that there's too many arguments in function call, or my object is not initialized.
How do I fix this? Thanks for your help!
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
class RatingCalculator
{
public:
virtual void showCountryTop() = 0;
virtual void getCountryTop() {};
};
class Beer
{
public:
string name;
string rating;
string country;
string alc;
string type;
};
class UnitedStatesBeer : public RatingCalculator
{
private:
string name;
string rating;
string country;
string alc;
string type;
public:
void showCountryTop() {};
void getCountryTop(vector<Beer> allBeers){};
int main()
{
ifstream file("beer.txt");
Beer currentBeer;
vector<Beer> allBeers;
for (int i = 0; !file.eof(); i++)
{
getline(file, currentBeer.name, '\t');
getline(file, currentBeer.rating, '\t');
getline(file, currentBeer.country, '\t');
getline(file, currentBeer.alc, '\t');
getline(file, currentBeer.type, '\n');
allBeers.push_back(currentBeer); //copy all the information to allBeers vector
}
file.close();
/*if I do it this way*/
UnitedStatesBeer UsReassign;
UsReassign->getCountryTop(allBeers); //<- expression (UsReassign) must have pointer type/ Using uninitialized memory
// RatingCalculator* UsReassign = new UnitedStatesBeer();
// UsReassign-> getCountryTop(allBeers); //<- too many arguments in function call
}

First off, Since you want to override virtual function, you'd want to match function definition in Base Class aka RatingCalculator with the one in UnitedStatesBeer. This is how it should look in RatingCalculator :
virtual void getCountryTop(vector<Beer> allBeers) {};
and in UnitedStatesBeer
void getCountryTop(vector<Beer> allBeers){};
Secondly, since you want to get Top beers, you may want to pass by reference, by adding & in front of variable beers.
Thirdly,
UsReassign->getCountryTop(allBeers); //<- expression (UsReassign) must have pointer type/ Using uninitialized memory..
This is happening because UsReassign is a object and NOT a pointer. Objects are referenced with . instead of ->.
And Lastly,
// UsReassign-> getCountryTop(allBeers); //<- too many arguments in function call
This is happening because function getCountryTop() with NO arguments is being called. Steps mentioned in "Firstly" should fix it.

Related

Multiple Definition Error in C++ using different files for declaration of function/vector

Sorry for this extensive (body)question, but I'm having an issue trying to separate my main() function from my read.cc file.
At first I wrote my main() in my read.cc for it was easier to test and it worked perfectly. Now I'm modularizing my code and using main() in a different file (main.cc), but I get a "Multiple definition error".
//course.h
#ifndef _COURSE_H
#define _COURSE_H
#include "dependencies.h"
class Course{
public:
int id;
std::string id_if;
std::string name;
std::string dayTime;
};
#endif
//read.h
#ifndef _READ_H
#define _READ_H
#include "../Classes/course.h"
#include "../Classes/dependencies.h"
using namespace std;
vector <Course*> course;
void readCourse();
void courseCheck(Grade* a, string* temp);
void dispoCheck(Teacher* teacher, string* temp, int day);
const vector<string> explode(const string& s, const char& c);
#endif
//read.cc
void readCourse(){
Course* inp = new Course();
ifstream file;
file.open("../Data/Cursos.csv");
string temp;
getline(file, temp, '\n');
while(file.good()){
getline(file, temp, ';');
inp->id = stoi(temp);
getline(file, inp->id_if, ';');
getline(file, inp->name, ';');
getline(file, inp->dayTime, '\n');
}
course.push_back(inp);
}
//main.cc
#include "Input/read.h"
int main(){
readCourse();
}
Error:
g++ main.cc Input/read.cc -o exe -lm
/tmp/ccyRgnlM.o:(.data+0x0): multiple definition of `course'
/tmp/ccoTZsnQ.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
From your code, it's not clear what you are trying to do. Since course is only every used inside the readCourse method, so could be declared locally inside the function.
I'd assume that (not shown) you want to also use it in main.
Since this is C++ (rather than C) then global variables are best avoided. Your course variable should be a static member of a class, possibly the Course class itself (and since it's a list, it's better with a plural name). Also, your readCourse method should be a static member, e.g.
//course.h
class Course{
public:
int id;
std::string id_if;
std::string name;
std::string dayTime;
static std::vector<Course*> courses;
static void readCourse();
};
// course.cc
void Course::readCourse(){
...
}
std::vector<Course*> courses; // Defines you single courses object
then in main() you can access it with:
int main(){
Course::readCourse();
Course::courses[0]->doSomething();
}
}

Manipulating vectors inside class methods

I just started learning about vectors, and they seem really handy, if talking about lists from a file. But I'm having a problem while trying to return a new vector from a class method and put it to another method of the same class.
i.e.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
class A
{
public:
string name;
string age;
};
class Test
{
string name;
string age;
public:
void get(vector<A> students)
{
vector<A> specificStudents;
//rewrite a couple of students from vector<A>students
//to vector<A> specificStudents
};
void set(vector<A> specificStudents)
{
//I need to get vector<A> specificStudents here
};
};
int main()
{
vector<A> students;
A stud;
ifstream file("file.txt");
for (int i = 0; i < file.eof(); i++)
{
getline(file, stud.name);
getline(file, stud.age);
students.push_back(stud);
};
Test test;
test.get(students);
return 0;
}
Can I return it as a function argument?
It is not entirely clear, but do you want to save a copy of students inside test?:
class Test
{
vector<A> specificStudents;
public:
const vector<A>& get() const
{
return specificStudents;
}
void set(const vector<A>& students)
{
specificStudents = students;
// maybe modify `specificStudents` here in some way or assign it differently
}
};
int main()
{
vector<A> students;
A stud;
ifstream file("file.txt");
while (getline(file, stud.name) && getline(file, stud.age))
{
students.push_back(stud);
}
Test test;
test.set(students);
//... Do something with test
return 0;
}
If you don't know yet what & and const mean in this, you can just remove them (until you learn about them).
You seem to have gotten get and set mixed up. By usual convention, get gets something stored in the class object to the caller and set sets something in the class object (with a value provided by the caller).
I also fixed your input loop. I don't know what you thought file.eof() does, but comparing to i does not do anything useful.
Also be aware of where ; belongs and where it doesn't. It belongs after single statements and class definitions, but not after } of function definitions or other statement blocks, such as the one of while.
YES, you can return vector from a function.
To do this you can write a function like this one:
std::vector<A> get(vector<A> students)
and the inside it return the vector. An example can be:
vector<A> get(vector<A> students)
{
vector<A> specificStudents;
//rewrite a couple of students from vector<A>students
//to vector<A> specificStudents
return specificStudents;
};

naming objects by variables

I am in a situation that I need to make objects in runtime that are named by value of a string, but I can't do that:
cin>>input;
className "input"= new className;
How can I do that?
I think it is possible to achieve by using maps. Is it true?
As you said, you can achieve your goal by using std::map (or std::unordered_map)
map<string, className*> aMap;//map a string to a className pointer/address
cin>>input;
aMap[input] = new className; //input is mapped to a className pointer
Then you can treat aMap[input] as a className*. e.g.
To call a className method, you can:
aMap[input]->aClassNameMethod();
The object oriented way would be to make name a member of that class and use the input to construct the class.
#include <string>
#include <iostream>
class Foo
{
std::string myName;
public:
// Constructor assigning name to myName.
Foo(const std::string name) : myName(name) {}
std::string GetMyName() const
{
return myName;
}
};
int main()
{
std::string input;
std::cin >> input;
Foo f(input);
std::cout << f.GetMyName();
}
Also read about new in C++: Why should C++ programmers minimize use of 'new'?

Cannot display file(related to classes)

So I am trying to read in a file using private class variables. I am unsure how to display the file. There might be another way to do this, but this is what I could think of. Note, its my first project using classes and private and public/private members. Was I on the right path atleast? I keep getting an error for the int main function. How can I fix it?
This is my main:
#include "Record.h"
#include <sstream>
int main ()
{
Record employee;
ifstream myFile;
myFile.open("Project 3.dat");
string str;
int i=0;
if (myFile.is_open())
{
while (getline(myFile, str))
{
istringstream ss(str);
ss >> employee.get_name(str) >> employee.get_id(stoi(str)) >>
employee.get_rate(stoi(str)) >> employee.get_hoursWorked(stoi(str));
}
}
return 0;
}
This is my header:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Record
{
private:
string name;
int id;
double rate;
double hours;
public:
Record();
Record (string n, int empid, double hourlyRate, double hoursWorked);
// constructor
void read_data_from_file();
double calculate_wage();
void print_data();
/* ASETTERS AND GETTERS */
void set_name (string n);
string get_name();
void set_id (int empid);
int get_id();
void set_rate (double hourlyRate);
double get_rate();
void set_hoursWorked(double hoursWorked);
double get_hoursWorked();
/* END OF SETTERS AND GETTERS */
};
This is my cpp
#include "Record.h"
Record::Record():name(), id(0), rate(0), hours(0) {} //default constructor
must be implemented first
Record::Record(string n, int empid, double hourlyRate, double hoursWorked)
{
name = n;
empid = id;
hourlyRate = rate;
hoursWorked = hours;
}
//
void Record::set_name(string n)
{
name = n;
}
string Record::get_name()
{
return name;
}
//
void Record::set_id(int empid)
{
id = empid;
}
int Record::get_id()
{
return id;
}
//
void Record::set_rate(double hourlyRate)
{
rate = hourlyRate;
}
double Record::get_rate()
{
return rate;
}
//
void Record::set_hoursWorked(double hoursWorked)
{
hours = hoursWorked;
}
double Record::get_hoursWorked()
{
return hours;
}
//
double Record::calculate_wage()
{
return (rate * hours);
}
There are some issues with your code that I can see. most of your problems aren't related to your question (I mean using a class or private/public members). you have more basic misunderstandings. So here's some explanation that might help you:
1- Using functions : You have some troubles using your defined functions, A function can have multiple input parameters and one return value. basically it's like this return_type function_name(parameter_type param1, ...). it means that if you call this function you need to pass param1,... and expect your function operation and then have a return value of return_type. You defined some set and get functions. if you want to set something you should call set function and pass your desired value to it and it will copy your value to your defined member data, after that you can call get function to retrieve that value. So when you call get function with parameter it will raise error. Here you want to call set function.
2- Using stoi : As you can see you are getting error on using stoi function too, this is a function for converting string to integer, The thing that you missed here is that this function declared in std namespace. If you want to use it you need to use it like this std::stoi(str). one other thing, using namespace std is a bad practice.
3- Design matters : In OOP design, a class must have a purpose and an actual job to do. It might be an interface to abstract class but a bunch of set and get functions will not fulfill the need to create a class. Here if your class is going to do file operations, it's OK, but as far as you shared your code it's just some set and get functions.

error: incompatible types in assignment of ‘char’ to ‘char [11]’

I'm trying to create a member function that allows an user to set member array variables.
I've been looking everywhere but I can't find the problem in my code,
#include <string>
#include <iostream>
using namespace std;
class Employee
{
protected:
string name;
char ssn[11];
char id[5];
char hired[8];
public:
Employee(char ssn, char id, char hired); //Constructor
Employee(string name);
~Employee(); //Destructor
void setName(string n) { n = name; }
void setSSN(char i) { ssn = i; }
};
int main()
{
return 0;
}
Let's have a look at your setSSN function:
void setSSN(char i) { ssn = i; }
SNN, which most likely means social security number, doesn't consist of just one digit but 11, right? Then why would setSSN take as input only one character (digit) by (char i)? So setSSN function should rather take a string of characters containing SSN of the employee and that string should be of the same flavor as the ssn member variable of your Employee class in order to let you assign one string variable by another in the body of setSSN function. If you are already familiar with the string class of the C++ standard library, you should probably use that class for all your string storage and manipulation.