an error "has no member named" - c++

I have this snippet of the code
account.cpp
#include "account.h"
#include <iostream>
#include <string>
using namespace std;
Account::Account(string firstName, string lastName, int id)
: strFirstName(firstName), strLastName(lastName), nID(id) {}
void Account::printAccount(){
cout << strFirstName;
}
account.h
#include <string>
using std::string;
class Account{
private:
string strLastName; //Client's last name
string strFirstName; //Client's first name
int nID; //Client's ID number
int nLines; //Number of lines related to account
double lastBill;
public:
Account(string firstName, string lastName, int id);
void printAccount();
};
company.h
#ifndef CELLULAR_COMPANY_H
#define CELLULAR_COMPANY_H
#include <string>
#include <list>
#include <iostream>
#include "account.h"
using namespace std;
class Company {
private:
list<Account> listOfAccounts;
public:
void addAccount(string firstName, string lastName, int id) {
Account newAccount(firstName, lastName, id);
listOfAccounts.push_back(newAccount);
}
void printAccounts(){
for(list<Account>::iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i){
i.printAccount; //here bug
}
}
};
#endif // CELLULAR_COMPANY_H
main.cpp
#include "cellularcompany.h"
int main(){
Company newCompany;
newCompany.addAccount("Pavel", "Nedved", 11111);
newCompany.printAccounts();
return 0;
}
can somebody please explain what does my error mean? thanks in advance (I have it in company.h see comment there)
I have bug 'struct std::_List_iterator<Account>' has no member named 'printAccount'

You forgot the parentheses after printAccount(). Otherwise, it's not a method call. Also, you need to use the -> operator, since it's an iterator.
for(list<Account>::iterator i = listOfAccounts.begin();
i != listOfAccounts.end(); ++i)
{
i->printAccount(); // Note the ()!
// This is equivalent to (*i).printAccount();
}

Try to change i.printAccount; to i->printAccount();

Related

Why is the Default Parameter being used each time? Initializing classes

When I printed the variables passed through, the default is printed first, followed by what I want passed. So the final result remains the same. The initialization is found in Owner.h and Owner.cpp. Variables are passed starting from the Dog.cpp. I've also tried changing my print statements to Dog.owner... but the result was the same.
Owner.h
#define OWNER_H
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
class Owner {
private:
string name;
int age;
public:
Owner(string ownerName = "Lucy" , int ownerAge = 10); // default params
string getName();
int getAge();
};
#endif
Owner.cpp
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
// Getters
string Owner::getName() {return name;}
int Owner::getAge() {return age;}
// Constructors
Owner::Owner(string ownerName, int ownerAge) :name(ownerName), age(ownerAge) {
Owner::getName();
Owner::getAge();
}
Dog.h
#ifndef DOG_H
#define DOG_H
#include <iostream>
#include <string>
#include "Owner.h"
using namespace std;
class Dog {
private:
string breed;
int age;
Owner owner;
static int dogCount;
public:
Dog();
Dog(string, int);
// Getter and Setter methods
void setBreed(string var);
void setAge(int var);
string getBreed();
int getAge();
// Other
void printDogInfo();
static int getDogCount() {return dogCount;}
};
#endif
Dog.cpp
#include <iostream>
#include <string>
#include "Dog.h"
#include "Owner.h"
using namespace std;
// Constructors
Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
Dog::Dog() {
}
void Dog::printDogInfo() {
cout << "owner: " << owner.getName() << ", " << owner.getAge() << " yo" << endl << endl;
}
int main() {
Dog myDog1("Belle", 15);
myDog1.setBreed("Siberian Husky");
myDog1.setAge(2);
myDog1.printDogInfo();
return 0;
}
Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
By:
Dog::Dog(string ownerName, int ownerAge) : Owner(ownerName, ownerAge) {
dogCount++;
}
Probably, you also want to fix this:
Owner::Owner(string ownerName, int ownerAge) :name(ownerName), age(ownerAge) {
// Owner::getName(); not needed
// Owner::getAge(); not needed
}
Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
is equivalent to
Dog::Dog(string ownerName, int ownerAge) :
breed(),
owner()
{
Owner(ownerName, ownerAge); // Create temporary
dogCount++;
}
You probably want instead:
Dog::Dog(string ownerName, int ownerAge) :
breed(),
age(0),
owner(ownerName, ownerAge)
{
dogCount++;
}

Seg Fault when using ->GetString(" ") which is in a separate class

I'm practicing some basic C++ right now, and decided to create a class in a header file and the constructor, GetString, etc functions in a separate file.
When I create my object using
"Person Bob" and use "." the code works fine, but if I do Person* Bob, the SetName(x) function seg faults, when I use ->SetName(x, with x being a "abc" string or a string variable
Main.cpp
#include <iostream>
#include <string>
#include "namevalue.h"
using namespace std;
int main(){
Person Bob;
string temp = "bob";
Bob.SetName(temp);
Bob.SetMoney(3000);
cout << Bob.GetName() << " " << Bob.GetMoney() << endl;
return 0;
}
Person.h
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Person{
public:
Person();
Person(int money, string name);
void SetName(string y);
void SetMoney(int x);
int GetMoney();
string GetName();
private:
int money;
string name;
};
Person.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <array>
#include "namevalue.h"
using namespace std;
Person::Person(){
name = " ";
money = 0;
}
Person::Person(int x, string y){
SetName(y);
SetMoney(x);
}
void Person::SetMoney(int x){
money = x;
}
void Person::SetName(string x){
name = x;
}
int Person::GetMoney(){
return money;
}
string Person::GetName(){
return name;
}
If you declare a pointer variable, you need to populate it first with a valid instance. Otherwise, it is pointing to invalid memory and you will get the memory fault you are experiencing.
This should work.
Person* Bob = new Person();
Bob->SetName("Bob");
Bob->SetMoney(3000);
When you're finished, free the memory.
delete Bob;

no instance of overloaded function matches the argument list c++

I'm a little confused by this error that I'm having (I'm using VS2012).
Here's my code:
RecipeBook.h:
#ifndef RECIPEBOOK_H
#define RECIPEBOOK_H
#include "SingleRecipe.h"
using namespace std;
class RecipeBook
{
private:
vector<SingleRecipe> *recipe;
SingleRecipe *one;
public:
RecipeBook(vector<SingleRecipe> *recipe);
void addRecipe(SingleRecipe *one);
bool removeRecipe(string name);
vector <SingleRecipe> *returnListOfRecipes(double time);
};
#endif
SingleRecipe.h:
#ifndef SINGLERECIPE_H
#define SINGLERECIPE_H
#include <string>
#include <vector>
using namespace std;
class SingleRecipe
{
private:
string name;
vector<string> ingredients;
vector<string> method;
int numOfServing;
double time;
public:
SingleRecipe(string name, vector<string> ingredients, vector<string> method, int numOfServing, double time);
string getName();
void setName();
int getNumOfServing();
void setNumOfServing();
double getTime();
void setTime();
string toString();
};
#endif
BookAndRecipe.cpp:
#include "RecipeBook.h"
#include "SingleRecipe.h"
#include <sstream>
#include <math.h>
using namespace std;
vector <SingleRecipe> *RecipeBook::returnListOfRecipes(double time)
{
vector<SingleRecipe> *two;
for (int i = 0; i = recipe->size(); i++)
{
if (recipe[i].data()->getTime < time)
{
*two->push_back(recipe[i].pop_back());
}
}
return NULL;
}
Over at returnListOfRecipes() I get this error:
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=SingleRecipe, _Alloc=std::allocator<SingleRecipe>]" matches the argument list
argument types are: (void)
object type is: std::vector<SingleRecipe, std::allocator<SingleRecipe>> c:\Users\Ventus\Documents\Visual Studio 2012\Projects\Recipe\Recipe\BookAndRecipe.cpp 83 8
I suspect it might have something wrong with my for loop, but I'm not very experienced, so I might be doing something very wrong here.
I appreciate all help that's given!
pop_back() doesn't return a value, it just drops the last element of the container. You probably want:
*two->push_back(recipe[i].back());
recipe[i].pop_back();

Inheritance error: expected class-name before '{' token

I have tried every combination of #include statements that I can think of, and nothing is working. I am trying to write a basic inheritance program but i keep getting the error error: expected class-name before '}' token and I just do not know what to do about it anymore. I've tried having my main() include the .cpp file of the Executive class, however this error shows up. The program includes 5 types of employees all inherited from the Employee class, and I'm assuming that they are all the same error:
#include <iostream>
#include <string>
#include "Employee.cpp"
#include "Manager.cpp"
#include "Executive.cpp"
#include "Technical.cpp"
#include "Software.cpp"
#include "Test.cpp"
using namespace std;
int main()
{
Employee emp[3];
Executive emp0("John", "Doe", "VP", 100000.0, 1000000.0, 2000.0);
Software emp1("Vincent", "Giuliana", "Project Leader", 150000.0, 200000.0, 1000.0);
Test emp2("Lauren", "Wallis", "Overseer of Testing", 95000, 115000);
emp[0] = emp0;
emp[1] = emp1;
emp[2] = emp2;
for(int i=0; i<3; i++)
emp[i].displayInformation();
emp0.displayInformation();
emp1.displayInformation();
emp2.displayInformation();
return 0;
}
My Employee.h header file is as follows:
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include <string>
#include <iostream>
using namespace std;
class Employee
{
private:
string fName, lName, jobTitle;
double baseSalary, salary;
public:
Employee();
Employee(string fName, string lName, string jobTitle, double baseSalary);
void calculateSalary(double baseSalary);
void displayName();
void displayBSalary();
void displayJobTitle();
void displayInformation();
...
getters
...
...
setters
...
};
#endif // EMPLOYEE_H_INCLUDED
My Employee.cpp is:
#include <string>
#include <iostream>
#include "Employee.h"
using namespace std;
Employee::Employee()
{
fName = "";
lName = "";
jobTitle = "";
baseSalary = 000000;
}
...
void Employee::setBSalary(double bs) //sets base salary as parameter
{
baseSalary = bs;
}
The top of the Executive.h header class:
#ifndef EXECUTIVE_H_INCLUDED
#define EXECUTIVE_H_INCLUDED
#include <string>
#include <iostream>
//#include "Employee.h"
using namespace std;
class Executive : public Employee
{
private:
string fName, lName, jobTitle;
double baseSalary, salary, bonus, stockOption;
public:
...
};
#endif // Executive_H_INCLUDED
And last but not least, the Executive.cpp file...
#include
#include
#include "Executive.h"
using namespace std;
Executive::Executive()
{
fName = fN;
lName = lN;
jobTitle = jt;
baseSalary = bs;
bonus = b;
stockOption = so;
}
...
void Executive::setSO(double so) //sets stock option as parameter
{
stockOption = so;
}
I think that I have tried to include each header in each file and still, nothing. Any help would be appreciated, and I thank anyone very much in advance!
You must
#include "Employee.h"
in Executive.h, because the compiler must see the declaration of Employee, when a class inherits from it. So, just remove the comments from the #include

C++ overloaded member function error

hi i was making a program with 3 classes and when i was using a member initialization list i got an error saying "no instance of overloaded function "people::people" matches the specified type:
MAIN.cpp
#include <iostream>
#include "conio.h"
#include <string>
#include "birthday.h"
#include "people.h"
using namespace std;
void main(){
birthday birthObj (30, 06, 1987);
people me("The King",birthObj);
_getch();
}
BIRTHDAY.h
#pragma once
class birthday
{
public:
birthday(int d, int m, int y);
void printdate();
private:
int month;
int day;
int year;
};
BIRTHDAY.cpp
#include "birthday.h"
#include <iostream>
#include "conio.h"
#include <string>
using namespace std;
birthday::birthday(int d, int m, int y)
{
month = m;
day = d;
year = y;
}
void birthday::printdate()
{
cout << day << "/" << month << "/" << year;
}
PEOPLE.h
#pragma once
#include <iostream>
#include "conio.h"
#include <string>
#include "birthday.h"
using namespace std;
class people
{
public:
people(string x, birthday bo);
void printInfo();
private:
string name;
birthday dateOfBirth;
};
PEOPLE.cpp
#include "people.h"
#include <iostream>
#include "conio.h"
#include <string>
#include "birthday.h"
using namespace std;
people::people()
: name(x), dateOfBirth(bo)
{
}
void people::printInfo()
{
cout << name << " was born on ";
dateOfBirth.printdate();
}
People.cpp should be:
people::people(string x, birthday bo) : name(x), dateOfBirth(bo) { }
You havn't implemented the people(string x, birthday bo); constructor. in your PEOPLE.cpp, change
people::people()
: name(x), dateOfBirth(bo)
to
people::people(string x, birthday bo)
: name(x), dateOfBirth(bo)
Your constructor in PEOPLE.cpp has the wrong signature:
It should be:
people::people(string x, birthday bo)
Instead of:
people::people()
people::people()
: name(x), dateOfBirth(bo)
{
}
You have forgotten your arguments to this constructor.
poeple ctor declaration and definition doesn't match!