C++ "No matching function for call" error - c++

I'm using MinGW compiler. I don't understand why I am getting an error.
Error :
Multiple markers at this line
- candidate is:
- no matching function for call to 'InsultGenerator::InsultGenerator()'
- Line breakpoint: Insultgenerator_0hl14.cpp [line: 22]
Here is the cpp file:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Insultgenerator_0hl14.h"
using namespace std;
FileException::FileException(const string& m) : message(m){}
string& FileException::what(){ return message;}
NumInsultsOutOfBounds::NumInsultsOutOfBounds(const string& m) : message(m){}
string& NumInsultsOutOfBounds::what(){ return message;}
InsultGenerator::InsultGenerator(const InsultGenerator& ) {}
void InsultGenerator::initialize() const{
int cols(0);
InsultGenerator t1;
string insults;
string filename("InsultsSource.txt");
ifstream file(filename.c_str());
if(file.fail()){
throw FileException("File not read.");
}
while(file >> insults){
if(cols==0){
t1.colA.push_back(insults);
cols++;
} else if(cols==1){
t1.colB.push_back(insults);
cols++;
}else{
t1.colC.push_back(insults);
cols= cols -2;
}
}
for (int i=0;i<50;i++){
cout << i << t1.colA[i];
}
}
Here is the header file:
#ifndef INSULTGENERATOR_0HL14_H_
#define INSULTGENERATOR_0HL14_H_
#include <string>
#include <vector>
using namespace std;
class InsultGenerator{
public:
// InsultGenerator(vector<string>);
InsultGenerator(const InsultGenerator &);
void initialize() const;
string talkToMe() const;
vector<string> generate(const int) const;
int generateAndSave (const string, const int) const;
private:
vector<string> colA;
vector<string> colB;
vector<string> colC;
};
class FileException{
public:
FileException(const string&);
string& what();
private:
string message;
};
class NumInsultsOutOfBounds{
public:
NumInsultsOutOfBounds(const string &);
string& what();
private:
string message;
};
#endif

You have one Constructor for InsultGenerator: InsultGenerator(const InsultGenerator &);, but it has parameters. You need a Constructor without parameters, so the statement InsultGenerator t1; can call the corresponding Constructor correctly.

Because you not have contructor for InsultGenerator object.
in header file, you add InsultGenerator(); and in cpp file, you add
InsultGenerator::InsultGenerator() { }

Related

'OBJECT' not declared in scope

My first ever c++ program and i cant seem to figure out why it says that my 'object' was not declared in the scope. I've included the header files, i've prototyped my functions. i'm used to coding in java and the separation is pretty confusing to me.
The aim of the program is simple: create a class: Semester.
Semester: will have ID, name, credit, marks.
provide get/set methods to input and output from kb
Main
#include <iostream>
#include <stdio.h>
#include "Semester.h"
using namespace std;
int main()
{
Semester s1;
cout << "Please enter ID" << endl;
cin >> sl.setid();
cout << "ID: " << s1.getid();
}
cpp
#include <stdio.h>
#include <iostream>
#include "Semester.h"
using namespace std;
Semester::Semester(){
}
//setters
void Semester::setid(string t_id) {id = t_id;}
void Semester::setunit(string t_name) {unit_name = t_name;}
void Semester::setcredit(int t_credit){credit = t_credit;}
void Semester::setmark(int t_marks) {marks = t_marks;}
//getters
string Semester::getid() {return id;}
string Semester::getunit() {return unit_name;}
int Semester::getcredit() {return credit;}
int Semester::getmark() {return marks;}
header
#ifndef SEMESTER_H
#define SEMESTER_H
#include <stdio.h>
#include <iostream>
class Semester{
private: //variables
std::string id;
std:: string unit_name;
int credit, marks;
public:
Semester(); //constructor
//setters
void setid(std::string id);
void setunit(std::string name);
void setcredit(int credit);
void setmark(int marks);
//getters
std::string getid();
std::string getunit();
int getcredit();
int getmark();
};
#endif // SEMESTER_H
The following line is incorrect.
cin >> sl.setid();
Problems with that line:
You are calling a function that expects an argument with no arguments.
The return type of setid() is void. That cannot be used as the RHS of the stream extraction function.
What you need is:
std::string id;
cin >> id;
s1.setid(id);
Suggestion for improvement: Make all the get functions const member functions.
//getters
std::string getid() const;
std::string getunit() const;
int getcredit() const;
int getmark() const;
Make sure to update the .cpp file accordingly.

ERROR using a class as a vector type - "Undeclared identifier"

Hello Stack Overflow!
I have encountered a problem which I have tried to solve but have not succeeded with, therefore I am turning my head towards you programmers hoping for answers.
What I am suspecting the problem is, is that it has to do with my main.cpp which I haven't started coding in yet for reasons. Could be very wrong though.
Any tips/hints are appreciated!
The error is about this part located in the BankFunctions header
// Vector
static std::vector<accounts> users;
The error messages are listed at the bottom.
I have included the two class header files.
Main.cpp:
// Classes
#include "BankFunctions.h" // Handles the bank functions
#include "accounts.h" // Handles the customer accounts
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
int main() {
return 0;
}
Class 1: Accounts
Header:
#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "BankFunctions.h"
class accounts
{
public:
// Constructor
accounts(
unsigned int newId,
unsigned int newAge,
unsigned int newSSN,
std::string newFirstName,
std::string newLastName,
std::string newAdress,
std::string newEmail,
double newBalance
);
// Overload constructor
accounts(std::string eId, std::string eNAge, std::string eSSN, std::string eFName, std::string eLName, std::string eEmail, std::string eAdress, std::string eNBalance);
// Mutators
inline void setId(unsigned int i) { id = i; }
inline void setAge(unsigned int a) { age = a; }
inline void setSSN(unsigned int ssn) { SSN = ssn; }
inline void setFirstName(std::string FN) { firstName = FN; }
inline void setLastName(std::string LN) { lastName = LN; }
inline void setEmail(std::string em) { email = em; }
inline void setAdress(std::string adr) { adress = adr; }
inline void setBalance(double newBalance, bool t);
// Accessors
inline unsigned int getId() const { return id; }
inline unsigned int getAge() const { return age; }
inline unsigned int getSSN() const { return SSN; }
inline std::string getFirstName() const { return firstName; }
inline std::string getLastName() const { return lastName; }
inline std::string getEmail() const { return email; }
inline std::string getAdress() const { return adress; }
inline double getBalance() const { return balance; }
private:
// Customer account details
unsigned int id;
unsigned int age;
unsigned int SSN; // Social Security Number
std::string firstName;
std::string lastName;
std::string adress;
std::string email;
double balance;
};
Class 2: BankFunctions
Header:
#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "accounts.h"
class BankFunctions
{
public:
BankFunctions();
static void loadVector(); // Loads in customer account information into objects that get stored into a vector. (All the existing accounts get loaded in)
static void newCustomer(); // Create new customer account
static void existingCustomer(); // View customer account
static void deposit(unsigned int accId); // Deposit money function
static void withdraw(unsigned int accId); // Withdraw money function
// Edit customer account
static void editCustomerDetails(unsigned int accId);
// Related Functions
static void editAge(unsigned int accId);
static void editSSN(unsigned int accId);
static void editFirstName(unsigned int accId);
static void editLastName(unsigned int accId);
static void editAdress(unsigned int accId);
static void editEmail(unsigned int accId);
static void editBalance(unsigned int accId);
private:
// Vector
static std::vector<accounts> users;
static unsigned int amountOfAccounts;
};
ERROR Message:
...bankfunctions.h(36): error C2065: 'accounts': undeclared identifier
...bankfunctions.h(36): error C2923: 'std::vector': 'accounts' is not a valid template type argument for parameter '_Ty'
...bankfunctions.h(36): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type
Try just to remove the include BankFunctions.h from the accounts.h.
The problem, I think, is that an include performs simply an inserting of the whole header file at the position where the include is done, but pragma once ensures that you do not include a header twice into a cpp file.
When your headers include each other it is possible to get the class definition of accounts past the line 36 of BankFunctions.h where you need it.
You may also put an extra class accounts; on the very top of main.cpp. This should also work, but is not a pretty solution.

No variable member function declared in class?

can sombody explain to me why my code will not work, and how to fix it thanks :)
I keep recieving this error :
no 'int burrito::setName()' member function declared in class 'burrito'
My goal is to call a function from a different class file
My main.cpp :
#include <iostream>
#include "burrito.h"
using namespace std;
int main()
{
burrito a;
a.setName("Ammar T.");
return 0;
}
My class header (burrito.h)
#ifndef BURRITO_H
#define BURRITO_H
class burrito
{
public:
burrito();
};
#endif // BURRITO_H
My class file (burrito.cpp):
#include "burrito.h"
#include <iostream>
using namespace std;
burrito::setName()
{
public:
void setName(string x){
name = x;
};
burrito::getName(){
string getName(){
return name;
};
}
burrito::variables(string name){
string name;
};
private:
string name;
};
Your code is a mess. You need to write function prototypes in the header file and function definitions in the cpp file. You are missing some basic coding structures. See below and learn this pattern of coding:
This code should work and enjoy burritos !
main():
#include <iostream>
#include "Header.h"
int main()
{
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
getchar();
return 0;
}
CPP file:
#include "Header.h"
#include <string>
void burrito::setName(std::string x) { this->name = x; }
std::string burrito::getName() { return this->name; }
Header file:
#include <string>
class burrito
{
private:
std::string name;
public:
void setName(std::string);
std::string getName();
//variables(string name) {string name;} // What do you mean by this??
};
Your poor little burrito is confused. Confused burritos can't help much.
You may want your burrito declaration as:
class Burrito
{
public:
Burrito();
void set_name(const std::string& new_name);
std::string get_name() const;
private:
std::string name;
};
The methods could be defined in the source file as:
void
Burrito::set_name(const std::string& new_name)
{
name = new_name;
}
std::string
Burrito::get_name() const
{
return name;
}
The header file only has a constructor for the class. The member functions
setName(string) and getName()
are not declared in the header file and that is why you get the error.
Also, you need to specify the return type for functions.
One way to do this would be
//Header
//burrito.h
class burrito{
private:
string burrito_name;
public:
burrito();
string getName();
void setName(string);
}
//burrito.cpp
#include "burrito.h"
#include <iostream>
using namespace std;
string burrito::getName()
{
return burrito_name;
}
void burrito::setName(string bname)
{
bname =burrito_name;
}
This is a simple example for class in C++,
Save this in burrito.cpp file then compile and run it:
#include <iostream>
#include <string>
using namespace std;
class burrito {
public:
void setName(string s);
string getName();
private:
string name;
};
void burrito::setName(string s) {
name = s;
}
string burrito::getName() {
return name;
}
int main() {
burrito a;
a.setName("Ammar T.");
std::cout << a.getName() << "\n";
return 0;
}

Error with const member in class

I have a class named Student with its Name and Address classes.
#ifndef ADDRESS_H
#define ADDRESS_H
//This is address class
#include <string>
class Address{
public:
Address(std::string street, std::string city, std::string state, std::string zip) :
street(street), city(city),state(state),zip(zip)
{}
std::string street,city,state,zip;
std::string aString;
aString=street+city+state+zip;
//private:
};
#endif
and the Name class is
#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
class Name {
friend std::ostream &operator <<(std::ostream &os, const Name &name) {
if(name.middle!="")
os << name.last << ", "<<name.middle<<" ," << name.first;
else
os<< name.last <<", "<<name.first;
return os;
}
public:
Name(std::string last, std::string middle, std::string first) : last(last), first(first),middle(middle) {}
private:
std::string last, first, middle;
};
#endif
And the Student class is like:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
#include "name.h"
#include "Address.h"
class Person {
friend std::ostream &operator <<(std::ostream &os, const Person &person);
public:
Person(const Name &name, int age, const Address &address);
Address address;
std::string adr=address.aString;
//private:
Name name;
int age;
};
#endif
Finally, to call them.
#include <iostream>
#include "student.h"
#include <string>
using namespace std;
Person::Person(const Name &name, int age, const Address &address) : name(name), age(age),address(address) {}
ostream &operator <<(ostream &os, const Person &person) {
os << person.name << " " << person.age<<person.adr;
return os;
}
#include <iostream>
#include "student.h"
using namespace std;
int main() {
Person p(Name("Doe","","Jane"), 21, Address("Park Ave","New York","NY","10002"));
Person p2(Name("Bane","IHateM","Jane"), 21, Address("Bay parkway","Brooklyn","NY","11223"));
cout << p<<endl;
cout<< p2<<endl;
return 0;
}
However, there are some errors during compilation.
(1) The following line is wrong based on complier, how to fix it please?
std::string adr=address.aString;
(2) In my address class, the compiler said that "string does not name a type Error", but following this Why am I getting string does not name a type Error? can't fix the problem, why is that?
Simple Solution
The simplest solution is to move your aString=street+city+state+zip; inside the Address constructor.
Do the same for your adr = ... statement (you'll still need a 'declaration' for std::string adr; within your class header).
To understand why what you wrote won't work, consider this:
When you write (within a class declaration, like in your header)
class myClass
{
int a = 5;
};
you assign a default value to the int a that you have declared - this is both declaration and (default) assignment.
When you write
class Address{
public:
Address(std::string street, std::string city, std::string state, std::string zip) :
street(street), city(city),state(state),zip(zip)
{}
std::string street,city,state,zip;
std::string aString;
aString=street+city+state+zip;
};
you're trying to give a default assignment to aString, but this is invalid code.
You could do this using
std::string aString = ...;
but not
std::string aString;
aString = ...;
because the last line is a 'statement' - something to be executed.

Implicitly declared function error C++

I'm having an issue compiling my C++ file.
This is the error I get:
Multiple markers at this line
- Member declaration not found
- definition of implicitly-declared 'InsultGenerator::InsultGenerator(const InsultGenerator&)'
I'm using MinGW as my compiler.
Here is the C++ code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Insultgenerator_0hl14.h"
using namespace std;
FileException::FileException(const string& m) : message(m){}
string& FileException::what(){ return message;}
NumInsultsOutOfBounds::NumInsultsOutOfBounds(const string& m) : message(m){}
string& NumInsultsOutOfBounds::what(){ return message;}
InsultGenerator::InsultGenerator(const InsultGenerator& ) {}
void InsultGenerator::initialize() const{
int cols(0);
string x;
string filename("InsultsSource.txt");
ifstream file(filename.c_str());
if(file.fail()){
throw FileException("File not read.");
}
while(file >> x){
}}
//vector<string> InsultGenerator::talkToMe() const{
// };//end talkToMe
// vector<string> InsultGenerator::generate(const int n) const{
// };//end generate
//int InsultGenerator::generateAndSave(const string filename, const int n) const{
//};//end generateAndSave
Here is the header file:
#ifndef INSULTGENERATOR_0HL14_H_
#define INSULTGENERATOR_0HL14_H_
#include <string>
#include <vector>
using namespace std;
class InsultGenerator{
public:
InsultGenerator(vector<string>);
void initialize() const;
string talkToMe() const;
vector<string> generate(const int) const;
int generateAndSave (const string, const int) const;
private:
vector<string> colA;
vector<string> colB;
vector<string> colC;
};
class FileException{
public:
FileException(const string&);
string& what();
private:
string message;
};
class NumInsultsOutOfBounds{
public:
NumInsultsOutOfBounds(const string &);
string& what();
private:
string message;
};
#endif
You are implementing InsultGenerator's copy-constructor although you haven't declared it.
Add InsultGenerator(const InsultGenerator& ); to your InsultGenerator class.
Like so:
class InsultGenerator
{
public:
InsultGenerator(vector<string>); // also better remove that one since I don't
// think you have implemented it
InsultGenerator(const InsultGenerator &); // here
void initialize() const;
string talkToMe() const;
vector<string> generate(const int) const;
int generateAndSave (const string, const int) const;
private:
vector<string> colA;
vector<string> colB;
vector<string> colC;
};
Edit:
class InsultGenerator
{
public:
InsultGenerator(vector<string>); // Remove this line.
InsultGenerator(const InsultGenerator &); // Add this line.
void initialize() const;
string talkToMe() const;
......
}