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;
......
}
Related
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.
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.
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;
}
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() { }
TicTacToe.h
#pragma once
#include "Header.h"
class TicTacToe
{
vector<char>tictac_array;
Records records;
public:
TicTacToe();
~TicTacToe();
void push_tictac_array();
int wincheck();
void board(string player1, string player2);
void start();
void mainmenu();
};
Header.h
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <algorithm>
#include "Records.h"
#include "TicTacToe.h"
using namespace std;
Records.h
#pragma once
#include "Header.h"
class Records
{
protected:
vector<Records*> rec;
string player;
int wins;
int games;
int draws;
public:
Records();
Records(string player, int wins, int games, int draws);
~Records();
void setName(string player);
void setWins(int wins);
void setGames(int games);
void setDraws(int draws);
string getName();
int getWins();
int getGames();
int getDraws();
void addRec(string player, int wins, int games, int draws);
void sortStats();
void outRec();
void saveTo();
void readFrom();
};
main.cpp
#include "Header.h"
const int NotUsed = system("color F0");
void main()
{
setlocale(0, "rus");
TicTacToe tictac;
tictac.records.readFrom();
tictac.mainmenu();
}
Before the breakdown on the files and everything worked perfectly. Only after spread all classes file, began issuing this error, what could be the problem?
Added Records.h header file
How many confunsion.
First of all:
never use useless include in header files.
never use namespace in header files.
In TicTacToe.h you include "Header.h" that includes "TicTacToe.h" itself: recursion.
Second:
"#pragma once" is not standard, so I recommend to use instance:
#ifndef RECORDS_H_
#define RECORDS_H_
class Records {
}
#endif
If you really want to use "#pragma once", learn to use it before: it should be the first instruction, at least before all "inclues", generally.
Third:
use keyword 'private' even tough it is implicit;
get method should be const;
pass parameters by references, you avoid useless copy.
TicTacToe.h
#ifndef TICTACTOE_H_
#define TICTACTOE_H_
#include <vector>
#include "Records.h"
class TicTacToe {
private:
std::vector<char> tictac_array;
Records records;
public:
TicTacToe();
~TicTacToe();
void push_tictac_array();
int wincheck();
void board(const std::string& player1, const std::string& player2);
void start();
void mainmenu();
};
#endif
Records.h
#ifndef RECORDS_H_
#define RECORDS_H_
#include <vector>
class Records {
protected:
std::vector<Records*> rec;
string player;
int wins;
int games;
int draws;
public:
Record();
Record(const std::string& player, const int& wins, const int& games,const int& draws);
~Record();
void setName(const std::string& player);
void setWins(const int& wins);
void setGames(const int& games);
void setDraws(const int& draws);
string getName() const;
int getWins() const;
int getGames() const;
int getDraws() const;
void addRec(const std::string& player, const int& wins, const int& games,const int& draws);
void sortStats();
void outRec();
void saveTo();
void readFrom();
};
#endif
Main:
#include <clocale>
#include <cstdlib>
#include "TicTacToe.h"
using namespace std;
const int NotUsed = system("color F0");
void main() {
setlocale(0, "rus");
TicTacToe tictac;
tictac.records.readFrom();
tictac.mainmenu();
}
Fourth:
Records class is totally wrong: it has a std::vector of Records*, that make it recursive.
You should have instance:
Record.h
class Record {
protected:
string player;
int wins;
int games;
int draws;
public:
Records();
Records(const std::string& player, const int& wins, const int& games,const int& draws);
~Records();
void setName(const std::string& player);
void setWins(const int& wins);
void setGames(const int& games);
void setDraws(const int& draws);
string getName() const;
int getWins() const;
int getGames() const;
int getDraws() const;
void addRec(const std::string& player, const int& wins, const int& games,const int& draws);
void sortStats();
void outRec();
void saveTo();
void readFrom();
};
Records.h
class Records : public std::vector<Record*> {
}
This make more sense, for me at least.
You need to guard the Header.h file. Just put #pragma once at the top as you have done for Records.h