For some reason my, makefile will not compile. The error it is talking about is in my header.
Alignment.hh:34:22 error: expected ')' before '&' token
The calls are supposed to be using the references, which I have in when I call this function.
This is my Alignment.hh file
#ifndef encod
#define encod
class Encoded
{ private:
Direct &origin;
char *subinsertion;
int subinsertlen;
struct Edit *operation;
int editnum;
string dname;
int dlength;
int gaplen;
public:
Encoded(Alignment &obj);
~Encoded();
int getEditNum() const;
struct Edit* getOperation() const;
int getSubInsertLen() const;
char* getSubInsertion() const;;
int getDLength() const;
string getDName() const;
Direct& getOrigin() const;
string toString() const;
char* getDSeq() const;
int getNumDiff();
bool operator<=(Encoded &rightobj)const;
};
class Compressed : public Encoded {
private:
Encoded encode;
public:
Compressed(Alignment &obj);
bool operator <=(Encoded &rightobj) const;
};
#endif
Try to compile this
#ifndef encod
#define encod
class Direct;
class Alignment;
class Encoded
{ private:
Direct &origin;
char *subinsertion;
int subinsertlen;
struct Edit *operation;
int editnum;
string dname;
int dlength;
int gaplen;
public:
Encoded(Alignment &obj);
~Encoded();
int getEditNum() const;
struct Edit* getOperation() const;
int getSubInsertLen() const;
char* getSubInsertion() const;;
int getDLength() const;
string getDName() const;
Direct& getOrigin() const;
string toString() const;
char* getDSeq() const;
int getNumDiff();
bool operator<=(Encoded &rightobj)const;
};
class Compressed : public Encoded {
private:
Encoded encode;
public:
Compressed(Alignment &obj);
bool operator <=(Encoded &rightobj) const;
};
#endif
Related
class Point2d
{
public:
Point2d();
Point2d(int X, int Y);
~Point2d();
int m_x;
int m_y;
};
class Creature
{
private:
Point2d& m_yer;
const std::string& m_Name;
public:
Creature() = delete;
Creature(const std::string& name, Point2d& location) : m_Name(name), m_yer(location) {}
void Creature_Move(int x, int y);
~Creature();
};
int main()
{
std::string name = "Monke";
Point2d abc{ 25,30 };
Creature canavva(name, abc);
//Creature canavv(name, Point2d{ 40,50 }); //Gives error
return 0;
}
But when I put const in the argument Creature(const std::string& name, const Point2d& location) and const Point2d& m_yer; then
Creature canavv(name, Point2d{ 40,50 }) doesn't give error.
How can I initiliaze Poin2d without const keyword because I'm changing values inside it.
I'm preparing for an exam of c++. In the last test I failed the cohesion between a class that had the Date date_event as a variable.
How to create different constructors of class P if the class P need an object Data to create an object of the class( the one with no parameters, the one with all the parameters and the copy constructor)?
Also i didn't write this Date actual class. Is it correct to write constructors like that ?
How should giorno(0) , month(0) etc work properly ?
(giorno=day, mese=month, anno= year)
#ifndef DATA_H
#define DATA_H
#include <iostream>
#include <cstring>
using namespace std;
class Data{
friend ostream& operator<<(ostream&, const Data&);
friend istream& operator>>(istream&, Data&);
protected:
int giorno;
int mese;
int anno;
public:
Data(): giorno(0), mese(0), anno(0){}
Data(const int& g, const int& m, const int& a): giorno(g), mese(m), anno(a){}
Data(const Data& D) : giorno(D.giorno), mese(D.mese), anno(D.anno){}
//...
};
#include "Data.h"
class P{
protected:
Data birthday;
public:
P();
P(const int, const int, const int); //i should pass day, month, year right?
P(const P&);
}
Well the question is not clear, but several things can be said.
General
Avoid using namespace std; and use the namespace directly. E.g. friend std::ostream& operator<<(std::ostream&, const Data&);
I suppose this is not the original code, but just in case : don't forget your header guards #ifndef CLASSNAME_H, #define CLASSNAME_H & #endif /* CLASSNAME_H */
Date class
You can implement the constructor that way, but prefer implementing them in the .cpp file.
It is unnecessary to pass the integer parameters as references. Use this for big parameters, from strings to whole objects.
Prefer English for code, even if your colleagues/reviewers/professors are not English. It is always preferred in computer science. This is valid for variable/function names, but also for comments.
P(erson) class
Give your parameters names. Often people that will use your code will only read the headers, so the headers must be as self-explanatory as possible.
For the P contractor, try this :
class Date {
public:
Date(const int pDay, const int pMonth, const int pYear);
Date(const Date &pDate);
/* Getters */
int day(void) const;
int month(void) const;
int year(void) const;
/* Setters */
void setDay(const int pDay);
void setMonth(const int pMonth);
void setYear(const int pYear);
protected:
int mDay;
int mMonth;
int mYear;
};
class Person {
public:
Person(const int pDay, const int pMonth, const int pYear);
Person(const Person &pPerson);
/* Getters */
Date birthday(void) const;
/* Setters */
void setBirthday(const int pDay, const int pMonth, const int pYear);
void setBirthday(const Date &pDate);
protected:
Date mBirthday;
};
Date::Date(const int pDay, const int pMonth, const int pYear):
mDay(pDay),
mMonth(pMonth),
mYear(pYear)
{
/* Empty */
}
Date::Date(const Date &pDate) :
mDay(pDate.day()),
mMonth(pDate.month()),
mYear(pDate.year())
{
/* Empty */
}
int Date::day(void) const {
return mDay;
}
int Date::month(void) const {
return mMonth;
}
int Date::year(void) const {
return mYear;
}
/* Setters */
void Date::setDay(const int pDay) {
mDay = pDay;
}
void Date::setMonth(const int pMonth) {
mMonth = pMonth;
}
void Date::setYear(const int pYear) {
mYear = pYear;
}
Person::Person(const int pDay, const int pMonth, const int pYear):
mBirthday(pDay, pMonth, pYear)
{
/* Empty */
}
Person::Person(const Person &pPerson):
mBirthday(pPerson.birthday().day(), pPerson.birthday().month(), pPerson.birthday().year())
{
/* Empty */
}
Date Person::birthday(void) const {
return mBirthday;
}
void Person::setBirthday(const int pDay, const int pMonth, const int pYear) {
mBirthday.setDay(pDay);
mBirthday.setMonth(pMonth);
mBirthday.setYear(pYear);
}
void Person::setBirthday(const Date &pDate) {
mBirthday.setDay(pDate.day());
mBirthday.setMonth(pDate.month());
mBirthday.setYear(pDate.year());
}
Please note that there are lots of ways to do this in a prettier fashion, and with less data copies.
i have a problem to delete an "Member" from a "Group" by index:
#include <vector>
#include <string>
using namespace std;
class Member
{
public:
explicit Member(const string &name, const unsigned long &key) : m_name(name), m_key(key) {}
const string &getName() const {return m_name;};
const unsigned long &getKey() const {return m_key;};
private:
string m_name;
unsigned long m_key;
};
//------------------------------------------------------------------------
class Group
{
public:
explicit Group(const string &name) : m_name(name) {}
const string &getName() const {return m_name;};
void addMember(const Member &member) {m_member.push_back(member);};
const vector<Member> &getMember() const {return m_member;};
private:
string m_name;
vector<Member> m_member;
};
void main() {
vector<Group> group;
group.push_back(Group("Membergroup 1"));
group[0].addMember(Member("Mark", 123456));
group[0].addMember(Member("John", 234567));
group[0].getMember().erase(group[0].getMember().begin() + 1); //to delete John
}
Error: : passing 'const std::vector' as 'this' argument discards qualifiers [-fpermissive] group[_group].getMember().erase(group[_group].getMember().begin() + 1);
Can someone help me please?
The problem is here:
const vector<Member> &getMember() const {return m_member;};
The function getMember() (which I suggest to call as getMembers()) returns a const reference to the vector.
Since constness, the compiler prevents modifications.
You can fix just refactoring in the following way:
class Group
{
public:
// ...
const vector<Member>& getMembers() const { return m_member; }
vector<Member>& getMembers() { return m_member; }
// ...
I want to instantiate a class regarding its name. I found that Qt allows it with QMetaType::create.
I also found that we have to register the class using Q_DECLARE_METATYPE.
But I did everything needed (I think), and it didn't work.
That's my basic class :
#ifndef __BLOCK_HH__
# define __BLOCK_HH__
# include <QMetaType>
# include <QObject>
# include <string>
# include <vector>
# include "Util.hpp"
# include "Common.hh"
class Block
{
protected:
std::string _name;
uint32 _u32StartAddr;
uint32 _u32EndAddr;
uint8 *_pu8Content;
bool _bSure;
bool _bVirgin;
std::string _type;
std::vector<std::string> _errorCauses;
Block(const std::string&, uint32, uint32, uint8 *, const std::string&);
public:
Block(const std::string& = "", uint32 = 0, uint32 = 0, uint8 * = 0);
Block(const Block&);
virtual ~Block();
Block& operator=(const Block&);
void setName(const std::string&);
void setStartAddr(uint32);
void setEndAddr(uint32);
void setContent(uint8 *);
void sure();
void notSure();
void erase();
const std::string& getName() const;
uint32 getStartAddr() const;
uint32 getEndAddr() const;
uint8 *getContent() const;
bool isSure() const;
bool isVirgin() const;
const std::string& getType() const;
void addError(const std::string&);
void dumpError(std::ostream& = std::cerr) const;
virtual void dump(std::ostream& = std::cout) const;
virtual void parse();
virtual bool operator<(const Block&) const;
};
Q_DECLARE_METATYPE(Block)
#endif
That's one of the inherited class :
#ifndef __CSS_HH__
# define __CSS_HH__
# include "Block.hh"
typedef struct
{
uint32 boot_lma;
uint32 boot_vma;
uint32 boot_length;
uint32 boot_entry;
uint32 nb_of_param;
} t_css;
class CSS : public Block
{
uint32 _u32BootFlashAddr;
uint32 _u32BootSdramAddr;
uint32 _u32BootSize;
uint32 _u32Bep;
uint32 _u32CpuClock;
uint32 _u32SdramSize;
bool _bEncram;
public:
CSS(uint32 = 0, uint32 = 0, uint8 * = 0);
virtual ~CSS();
virtual void dump(std::ostream& = std::cout) const;
virtual void parse();
uint32 getBootFlashAddr() const;
uint32 getBootSdramAddr() const;
uint32 getBootSize() const;
uint32 getBEP() const;
uint32 getCpuClock() const;
uint32 getSdramSize() const;
bool encramEnabled() const;
};
Q_DECLARE_METATYPE(CSS)
#endif
And that's the piece of code I use to instantiate :
Block *Mapper::constructBlock(const std::string& name, uint32 u32Start, uint32 u32End, uint8 *pu8Content)
{
Block *block = 0;
int32 i32Id = QMetaType::type(name.c_str());
if (i32Id != QMetaType::UnknownType)
{
block = static_cast<Block *>(QMetaType::create(i32Id));
if (block)
{
block->setStartAddr(u32Start);
block->setEndAddr(u32End);
block->setContent(pu8Content);
block->parse();
}
}
return (block);
}
The name I give here will be "Block" for the class Block and "CSS" for the class CSS. But QMetaType::type always return QMetaType::UnknownType.
Maybe I forgot something to register the class. Have you an idea ?
Thanks
You also need to register the type with qRegisterMetaType() to make the name available at runtime. Q_DECLARE_METATYPE makes the type known at compile time to Qt functions based on templates.
I've been assigned the following template:
#include <map>
template <typename T>
class Catalog {
struct Item {
//..
};
std::map<int, Item*> items;
public:
Catalog(void);
Catalog(const Catalog&);
~Catalog(void);
bool IsEmpty(void) const;
int Size() const;
void Add(T*);
T* Remove(T*);
T* Find(T*);
typedef void (T::*pFunc) (const T&);
void Inspection(pFunc) const;
};
Next, there is an abstract Product class and three subclasses:
class Product {
protected:
unsigned int _id;
string _name;
public:
Product(const int& id, const string& name) : _id(id), _name(name) {};
virtual void Action(const Product& p) = 0;
virtual int hashCode() {
return _id*100;
};
unsigned int getId(void) const {return _id;};
string getName(void) const {return _name;};
};
class ProductA : public Product {
public:
ProductA(const int& id, const string& name) : Product(id, name) {};
virtual void Action(const Product& p) {
cout << "ahoj" << endl;
};
};
Finally, class ProductsCatalog that handles a Catalog instance:
class ProductsCatalog {
Catalog<Product> catalog;
public:
//..
void CatalogInspection(void) const {
catalog.Inspection(&Product::Action);
}
};
What I have trouble with is the Inspection method:
template <typename T> void Catalog<T>::Inspection(pFunc p) const {
for (std::map<int, Item*>::const_iterator it=items.begin(); it!=items.end(); ++it) {
it->second->Product->*p(*(it->second->Product));
}
};
I am getting the following error:
error C2064: term does not evaluate to a function taking 1 arguments
I've tried everything I could think of, without success. The following works as intended, but is obviously not abstract enough:
it->second->Product->Action(*it->second->Product);
Did you try
(it->second->Product->*p)(*(it->second->Product));
for calling the method?
The thread Calling C++ class methods via a function pointer seems to be related.