I am doing an Assignment on one of my Programming modules and im a bit stuck.
I keep getting the Error no Matching Constructors for initialization whenever i create an object of a certian class.
There might be some other errors to, im still busy with the setup of everything. Is my classes setup correctly?
SavingsAccount.h
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include "transaction.h"
#include <QString>
#include <QList>
#include <QDate>
class SavingsAccount
{
public:
SavingsAccount (QString name, QString num);
~SavingsAccount();
QList<Transaction> addTransaction(Transaction T);
double totalTransactionCost();
QString frequentTransactionType();
QList<Transaction> transactionsOnAdate(QDate date);
QString toString();
private:
QString m_CustomerName;
QString m_AccountNumber;
QList<Transaction> m_TransactionList;
};
#endif // SAVINGSACCOUNT_H
SavingsAccount.cpp
#include "savingsaccount.h"
#include <QString>
#include <QList>
#include <iostream>
using namespace std;
SavingsAccount::SavingsAccount(QString name, QString num){
m_CustomerName = name;
m_AccountNumber = num;
}
SavingsAccount::~SavingsAccount(){
}
QList<Transaction> SavingsAccount::addTransaction(Transaction t){
}
QString SavingsAccount::frequentTransactionType(){
}
QString SavingsAccount::toString(){
}
double SavingsAccount::totalTransactionCost(){
}
QList<Transaction> SavingsAccount::transactionsOnAdate(QDate date){
}
Main.cpp
#include <QCoreApplication>
#include "transaction.h"
#include "savingsaccount.h"
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
string accName;
int accNum;
cout << "Enter Account Name: "<< endl;
cin >> accName;
cout<<"Enter Account Number: " <<endl;
cin >> accNum;
SavingsAccount accholder1(accName,accNum);
return a.exec();
}
Thanks so much. Im really strugling with this subject so dont laugh ;-)
You're creating the object with a std::string, but your constructor is expecting a QString. As of Qt 4 (don't know if Qt 5 allows this), there's no implicit conversion from a std::string to QString.
Either pass a pointer to a null-terminated char array:
SavingsAccount accholder1(accName.c_str(), accNum);
or use directly a QString to read the account name.
EDIT: I've just noticed that your constructor expects 2 QStrings, yet you're passing an int as the second parameter. You can convert a number to a QString using the static function QString::number.
The problem is that somewhere you are creating an object without passing the correct parameters to the constructor.
For example if you have a class SomeClass with a constructor like this
SomaClass(Qstring s1)
you cannot create a SomeClass like this;
SomaClass t;
You have to create instead:
SomeClasst t("text");
Related
I am coding a little RPG(Role Playing Game)
Here is the situation: I created an object Personnage.
In my classes, I created a method atttaquer. But I would like that after calling my method attaquer it writes something like this: Goliath attaque David . But to that, I need to grab the name of the Object. Because the player may want to edit the name of Object (The personage name) before playing.
There is my code:
Personnage.h
#ifndef Personnage_h
#define Personnage_h
#include <string>
#include "Arme.h"
class Personnage{
//methods
public:
Personnage();
Personnage(std::string nomArme, int degatsArme);
Personnage(int vie, int mana);
// ~Personnage();
void recevoirDegats(int nbDegats);
void attaquer(Personnage &cible);
private:
// Attributs
int m_vie;
int m_magie;
std::string m_nom;
};
#endif
My Personnage.cpp code:
#include "Personnage.h"
#include <string>
#include <iostream>
void Personnage::recevoirDegats(int nbDegats){
m_vie -= nbDegats;
if (m_vie < 0) {
m_vie = 0;
}
}
void Personnage::attaquer(Personnage &cible){
cible.recevoirDegats(m_arme.getDegats());
// if David attacks Goliath I want to write std::cout << David << "attaque "<< Goliath << endl; but I do not know how to grab the name of the object after it's creation
}
There is my main.cpp
#include <iostream>
#include <string>
#include "Personnage.h"
//#include "Personnage.cpp"
#include <ctime>
using namespace std;
int main()
{
Personnage David, Goliath, Atangana("Ak47", 35);
Goliath.attaquer(David);
return 0;
}
If you want to give your objects names, it cannot be the variable names. They are only meant for the compiler and they are fixed. So you need to create a class that can have a name:
class NamedObject
{
private:
std::string m_name;
public:
const std::string& getName() const
{
return m_name;
}
void setName(const std::string& name)
{
m_name = name;
}
}
And if you want your classes to have a name, the easiest way would be to derive from it:
class Personnage : NamedObject {
Then you can say:
Personnage player1, player2;
player1.setName("David");
player2.setName("Goliath");
Alternatively, you can get those string from user input.
And if you need to address one by name:
std::cout << player1.getName() << " please make your move." << std::endl;
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;
I have a small problem. I'm currently writing a code for tournament handling and I came out with an idea that the best way to keep teams in order in memory will be a list.
Now, I'm trying to sort list cointaing Team class that is containg points records.
Here's the class declaration:
#include "player.h"
#include <string>
class Team {
Player** Gracz;
std::string Name;
int TP, STP;
int Total;
public:
Team();
Team(Player* gracz1, Player* gracz2, Player* gracz3, Player* gracz4, Player* gracz5, Player* gracz6, std::string name);
~Team();
void SetTeam();
void SetTeam(Player gracz1, Player gracz2, Player gracz3, Player gracz4, Player gracz5, Player gracz6, std::string name);
void SetTP(int tp);
void SetSTP(int stp);
std::string GetTeam();
int GetTotal();
int GetTP();
int GetSTP();
bool operator<(Team& team);
bool operator>(Team& team);
void PrintTeam();
};
And here's the program code:
#include <iostream>
#include "player.h"
#include "team.h"
#include <list>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
Player *p;
Team *t1, *t2, *t3, *t4;
list<Team> x;
list<Team>::iterator it;
p=new Player("a","a","a");
t1=new Team(p,p,p,p,p,p,"A");
t2=new Team(p,p,p,p,p,p,"B");
t3=new Team(p,p,p,p,p,p,"C");
t4=new Team(p,p,p,p,p,p,"D");
x.push_back(*t1);
x.push_back(*t2);
x.push_back(*t3);
x.push_back(*t4);
cout<<"Turniej: "<<endl;
for(it=x.begin();it!=x.end();++it)
cout<<" "<<(*it).GetTeam();
cout<<endl;
t1->SetTP(15);
t2->SetTP(4);
t3->SetTP(8);
t4->SetTP(8);
t3->SetSTP(15);
t4->SetSTP(65);
x.sort();
cout<<"Turniej: "<<endl;
for(it=x.begin();it!=x.end();++it)
cout<<" "<<(*it).GetTeam();
cout<<endl;
return 0;
}
So I'd like to sort list by firstly TPs and then by STPs and it's included in declaration of overloaded operator <. When I'm printing list, I get A,B,C,D before the sorting and the same after the sorting, instead of A,D,C,B after. Where's my mistake?
Thanks for help.
Here the object is copied, and its copy is pushed into a container:
x.push_back(*t1);
/* the same for others */
Here you modify the original object, but the copy in the container is unchanged:
t1->SetTP(15);
Here is my implementation
Film.h //header
#ifndef FILM_H
#define FILM_H
#include <QString>
class Film {
protected:
QString title;
double dailyRate;
public:
Film(QString ti,double dr);
virtual double calculateRental(int num)const;
};
#endif // FILM_H
Film.cpp
#include "film.h"
#include <QString>
Film::Film(QString ti,double dr){
title=ti;
dailyRate=dr;
}
double Film::calculateRental(int num)const {
return dailyRate*num;
}
main.cpp
#include <QtCore/QCoreApplication>
#include <QtCore/QTextStream>
#include "film.h"
using namespace std;
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QTextStream cout(stdout, QIODevice::WriteOnly);
Film f("Top Gun", 10.00); //create an instance of a film
cout <<f.calculateRental(2);
return a.exec();
}
how do I count the number of Film instances created? I know is something like that:
static int numOfFilms;
numOfFilms++;
how do I use the code?
It depends. If you replace Class with class, and QString has a conversion constructor from const char*, then yes.
I'm pretty sure I've included the qanda class, but when I try to declare a vector that contains it or a class of that type I get an error saying that qanda is undefined. Any idea what the problem might be?
bot_manager_item.h
#pragma once
#include "../bot_packet/bot_packet.h"
#include <vector>
class bot_manager_item;
#include "qanda.h"
#include "bot_manager.h"
class bot_manager_item
{
public:
bot_manager_item(bot_manager* mngr, const char* name, const char* work_dir);
~bot_manager_item();
bool startup();
void cleanup();
void on_push_event(bot_exchange_format f);
bool disable;
private:
void apply_changes();
bot_manager *_mngr;
std::string _name;
std::string _work_dir;
std::string _message;
std::string _message_copy;
std::vector<qanda> games;
qanda test;
char _config_full_path[2600];
};
qanda.h
#ifndef Q_AND_A
#define Q_AND_A
#include "users.h"
#include "..\bot_packet\bot_packet.h"
#include "bot_manager.h"
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <fstream>
class qanda
{
public:
qanda(bot_manager * manager, std::string name, std::string directory);
~qanda(){};
void room_message(std::string username, std::string user_message);
void timer_tick();
private:
// data members
std::string question;
std::string answer;
std::string directory;
std::string command_prefix;
std::string name;
Users users;
std::map <std::string, std::string> questions_and_answers;
int time_per_question; // seconds
int time_between_questions; // seconds
int timer; // milliseconds
bool is_delayed;
bool is_playing;
bot_manager * manager;
// functions
void new_question();
void send_message(std::string msg);
void announce_question();
void load_questions();
};
#endif
Solved: I ended up refactoring the code in such a way as to avoid the use of bot_manager within the qanda class.
I suspect a circular #include problem. Is it possible qanda.h indirectly includes bot_manager_item.h?
It looks like you may be able to reduce header dependencies by using a forward declaration
class bot_manager;
instead of #include "bot_manager.h" in one or both of your posted header files.