C++ list, operator == problems - c++

Yeah i forgot about including header file.
I added inline bool operator== as you suggested but still bunch of troubles showed up themselves. Maybe i should stop for now but i don't want :D
By the way, getTytul() returns string.
So here is what i get:
header:
#include "Pozycja.h"
#include "IZarzadzaniePozycjami.h"
#include <iostream>
#include <list>
inline bool operator==(std::string& s, Katalog& katalog)
{
return katalog == s;
}
class Katalog
: public IZarzadzaniePozycjami
{
private:
std::string dzialTematyczny;
public:
void ZnajdzPozycjePoTytule(std::string tytul);
void ZnajdzPozycjePoId(int id);
void WypiszWszystkiePozycje();
Katalog(void);
Katalog(std::string dzialTematyczny_);
void DodajPozycje(Pozycja);
std::list<Pozycja> lista;
~Katalog(void);
};
cpp:
#include "Katalog.h"
#include <iterator>
Katalog::Katalog(void)
{
dzialTematyczny= "Nieznany dzial tematyczny";
}
Katalog::Katalog(std::string dzialTematyczny_):dzialTematyczny(dzialTematyczny_){}
void Katalog::DodajPozycje(Pozycja a){
std::cout << " Dodano pozycje";
lista.push_back(a);
}
void Katalog::ZnajdzPozycjePoTytule(std::string tytul){
for(std::list<Pozycja>::iterator iter = lista.begin(); iter!= lista.end();++iter)
{
if(tytul == iter->getTytul())
{
//std::cout << " Mamy tytul: "<< iter->getTytul() << std::endl;
}
}
}
void Katalog::ZnajdzPozycjePoId(int id){
for(std::list<Pozycja>::iterator iter = lista.begin(); iter!= lista.end();++iter)
{
if(id == iter->getId())
{
std::cout << " Mamy id: "<< iter->getId() << std::endl;
}
}
}
void Katalog::WypiszWszystkiePozycje(){
for(unsigned int i=0; i<lista.size(); ++i)
{
lista.front().WypiszInfo();
}
}
Katalog::~Katalog(void)
{
}
Errors:
1>c:\users\komputer\documents\visual studio 2012\projects\project1\katalog.h(6): error C2061: syntax error : identifier 'Katalog'
1>c:\users\komputer\documents\visual studio 2012\projects\project1\katalog.h(7): error C2805: binary 'operator ==' has too few parameters
1>c:\users\komputer\documents\visual studio 2012\projects\project1\katalog.h(8): error C2065: 'katalog' : undeclared identifier

Two immediate problems: You have not declared the IZarzadzaniePozycjami class anywhere. And having the equality operator in the class forces you to have an instance of the class on the left side of the comparison, and the argument on the right side.
The first you can solved by including the file where IZarzadzaniePozycjami is defined.
The second you can solved by adding a standalone and global helper function:
inline bool operator==(const std::string& s, const Katalog& katalog)
{
return katalog == s; // "Reverse" the arguments
}
Note: I made the function above inline so you can add it to the header file.

The first error message says that in the class definition
class Katalog
: public IZarzadzaniePozycjami
{
//...
the compiler knows nothing about definition of its base class IZarzadzaniePozycjami
Maybe you forgot to include the header with the corresponding definition.
The second error means that in the condition
if(tytul == iter->getTytul())
where tytul has type std::string the right operand that is returned by functionj iter->getTytul() has no type std::string.

Related

declare extern class c++ in hpp file and use it cpp file

I have two classes : Individu and Cite and as u can see Individu is defined before
//file.hpp
#include <iostream>
#include <stdexcept>
#include <vector>
extern Cite CITE;
class Individu {
protected:
static int id;
TYPE t;
public:
Individu();
virtual ~Individu();
static int & getCompteur();
virtual void afficher(std::ostream& ) const;
virtual TYPE getType() const;
};
class Cite {
std::vector<const Individu *> tab;
public:
Cite();
~Cite();
void addPersonne(const Individu *);
int size() const;
};
std::ostream& operator<<(std::ostream&, const Individu& );
#endif
I need to add an Individu one it's instanciated to the tab vector of Cite and sisnce there is just one Cite I declared Exctern CITE Cite to work with just like that :
// file.cpp
#include <algorithm>
#include "file.hpp"
int Individu::id = 0;
Individu::Individu() {
CITE.addPersonne(*this);
id++;
}
Individu::~Individu(){
}
int& Individu::getCompteur() {
return id;
}
void Individu::afficher(std::ostream& o) const{
o << id;
}
void Personne::afficher(std::ostream& o) const {
o << nom << " " << id;
}
std::ostream& operator<<(std::ostream& o, const Individu& i ){
i.afficher(o);
return o;
}
TYPE Individu::getType() const {
throw IllegalException();
}
Cite::Cite(){
}
Cite::~Cite() {
}
void Cite::addPersonne(const Individu * i){
tab.push_back(i);
}
int Cite::size() const {
return tab.size();
}
and when I compile I got this error :
file.hpp:13:8: error: ‘Cite’ does not name a type
13 | extern Cite CITE;
| ^~~~
file.cpp: In constructor ‘Individu::Individu()’:
file.cpp:30:5: error: ‘CITE’ was not declared in this scope
30 | CITE.addPerconne(*this);
| ^~~~
make: *** [makefile:15 : build/deviant.o] Erreur 1
I understand that Cite is not yet defined so that's why I got that error , so hwo can I fix it ?
You have two issues in your code:
extern Cite CITE is declared before the class Cite is defined, so the compiler doesn't know what a Cite is at that point. You should move this declaration after the definition of Cite.
You never define CITE. An extern variable declaration is a promise to the compiler that you will define that variable later. You're essentially saying "I promise a Cite object named CITE exists even though you can't see it right now". You broke that promise by never actually creating that object. You need to define a Cite CITE somewhere (most likely in file.cpp).

C++ Builder Pattern with Forward Declaration [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I recently got stuck in a situation. In the first version, everything is implemented in header file and it works fine. In the second version when i tried to separate implementation from header declarations, I got many errors. In the below lines, i m going to demonstrate problem. Thanks in advance..
First Version (it works fine!)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
public:
class CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
private:
CameraValue(const CameraProperties& properties)
:mProperties(properties)
{}
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Second Version (Don't work)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties;
public:
class CameraBuilder;
private:
CameraValue(const CameraProperties& properties);
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
cameravalue.cpp
#include "cameravalue.h"
#include <string>
class CameraValue::CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
class CameraValue::CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
CameraValue::CameraValue(const CameraProperties& properties)
: mProperties(properties)
{}
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Compile Errors
cameravalue.cpp
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\cameravalue.cpp(74)
: error C2440: 'initializing' : cannot convert from 'const
CameraValue::CameraProperties' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
..\BuilderPattern\cameravalue.cpp(74) : error C2439:
'CameraValue::mProperties' : member could not be initialized
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : see declaration of 'CameraValue::mProperties'
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\main.cpp(9) : error
C2440: '<function-style-cast>' : cannot convert from 'int' to
'CameraValue::CameraBuilder'
Source or target has incomplete type ..\BuilderPattern\main.cpp(9) : error C2228: left of '.setName' must
have class/struct/union ..\BuilderPattern\main.cpp(9) : error C2228:
left of '.build' must have class/struct/union
..\BuilderPattern\main.cpp(9) : error C2512: 'CameraValue' : no
appropriate default constructor available
..\BuilderPattern\main.cpp(10) : error C2039: 'getName' : is not a
member of 'CameraValue'
c:\users\huseyin\documents\builderpattern\cameravalue.h(8) : see declaration of 'CameraValue'
Definition of class CameraBuilder should be visible in main.cpp, so you can't just forward-declare it in cameravalue.h. But you can make the definitions of its member functions out-of-line:
// cameravalue.h
class CameraValue {
class CameraBuilder {
public:
CameraBuilder(int id);
...
};
};
// cameravalue.cpp
CameraValue::CameraBuilder::CameraBuilder(int id) {
...
}

Why can't a lambda (capturing 'this') in a member function-try-block handler access private data members in VC++ 2013?

Not the same as but possibly related to this question about static initializers.
Here the first two functions compile fine, and the last one doesn't in vc++ but does in clang and gcc:
class A {
protected:
std::string protected_member = "yay";
public:
void withNormalBlock();
void withFunctionBlock();
void noLambda();
};
void A::withNormalBlock() {
try {
throw std::exception();
} catch (...) {
[this]() {
std::cout << protected_member << std::endl;
}();
}
}
void A::noLambda() try {
throw std::exception();
} catch (...) {
std::cout << protected_member << std::endl;
}
void A::withFunctionBlock() try {
throw std::exception();
} catch (...) {
[this]() {
// this line is the problem:
std::cout << protected_member << std::endl;
}();
}
in clang (OK)
in gcc (OK)
in vc++ (error C2248: 'A::protected_member' : cannot access protected member declared in class 'A')
vc++ 2015 -- same deal
I can't find anything in the standard to suggest that the handler / catch block of a function-try-block should be exempt from function scope or that the lambda's closure type should change. The code compiles if the access type is changed to all public.
What could the root cause be? Is it a bug, or is it something specific to compiler settings that could be changed?
I'd guess it is a compiler bug. It reports the same error in VS2015 as well. Curiously, an attempt to explicitly simulate the functionality of lambda works without any issues in VS2015
class A
{
protected:
std::string protected_member = "yay";
public:
void withFunctionBlock();
};
void A::withFunctionBlock() try
{
throw std::exception();
}
catch (...)
{
struct Closure {
Closure(A *this_) : this_(this_) {}
void operator ()() const { std::cout << this_->protected_member << std::endl; }
A *this_;
};
Closure(this)();
}
I wonder what VS C++ compiler does differently under the hood...
Seems that this is a bug and the lambda in the catch scope is generated outside class scope. I tried to prove that with typeids but the Visual Studio lambda names are weirdly mangled and the name itself does not prove anything. However, error codes generated by the following snippet show that the names differ:
#include <iostream>
#include <typeinfo>
class Foo {
private:
public:
void testLambda()
try {
auto tryScope = [this]() {};
void (*p)() = tryScope;
}
catch(...)
{
auto catchScope = [this]() {};
void (*p)() = catchScope;
}
};
Error output:
(10): error C2440: 'initializing' : cannot convert from 'Foo::testLambda::<lambda_8a3a8afea7359de4568df0e75ead2a56>' to 'void (__cdecl *)(void)'
(15): error C2440: 'initializing' : cannot convert from '<lambda_8cbc08e7748553fb5ae4e39184491e92>' to 'void (__cdecl *)(void)'

unable to initialize Static const string

I have a class "GameOverState" which has a private member
static const std::string s_gameOverID;
In GameOverState.cpp I am initialising as :
const std::string GameOverState::s_gameOverID = "GAMEOVER";
I am getting the following errors:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2440: 'initializing' : cannot convert from 'const char [9]' to 'int'
error C2377: 'std::string' : redefinition; typedef cannot be overloaded with any other symbol
error C2373: 's_gameOverID' : redefinition; different type modifiers
error C2143: syntax error : missing ';' before 'GameOverState::s_gameOverID'
I have a PlayState class/PauseState class which have the same implementation which are working fine. How do I fix this bug??
GameOverState.h
#pragma once
#include "GameState.h"
#include "PlayState.h"
#include "MenuState.h"
#include "PauseState.h"
#include "AnimatedGraphic.h"
#include <string>
class GameObject;
class GameOverState : public GameState
{
public:
virtual void update();
virtual void render();
virtual bool onEnter();
virtual bool onExit();
virtual std::string getStateID() const { return s_gameOverID; }
private:
static void s_gameOverToMain();
static void s_restartPlay();
static const std::string s_gameOverID;
std::vector<GameObject*> m_gameObjects;
}
GameOverState.cpp
#include "GameOverState.h"
const std::string GameOverState::s_gameOverID = "GAMEOVER";
void GameOverState::s_gameOverToMain()
{
TheGame::Instance()->getStateMachine()->changeState(new MenuState());
}
void GameOverState::s_restartPlay()
{
TheGame::Instance()->getStateMachine()->changeState(new PlayState());
}
bool GameOverState::onEnter()
{
if (!TheTextureManager::Instance()->load("assets/gameover.png", "gameovertext", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/main.png", "mainbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/restart.png", "restartbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
GameObject* gameOverText = new AnimatedGraphic(new LoaderParams(200, 100, 190, 30, "gameovertext"), 2);
GameObject* button1 = new MenuButton(new LoaderParams(200, 200, 200, 80, "mainbutton"), s_gameOverToMain);
GameObject* button2 = new MenuButton(new LoaderParams(200, 300, 200, 80, "restartbutton"), s_restartPlay);
m_gameObjects.push_back(gameOverText);
m_gameObjects.push_back(button1);
m_gameObjects.push_back(button2);
std::cout << "entering PauseState\n";
return true;
}
You're missing the semicolon after the definition of GameOverState.
The preprocessor runs before compilation and basically just copy pastes the content of the header in the source file, altough we can't see that. An error resulting from a broken header can thus be pretty misleading.
It's legal to have class definitions inside a variable definition and the position of specifiers (like static) is not limited to the beginning of a declaration, either (for example, int const static x = 0; is fine).
So, your code looks like this to the compiler:
class GameOverState {} static const std::string GameOverState::s_gameOverID = "GAMEOVER";
Hopefully the errors make more sense now.
As everyone else has said, you're probably missing the #include line in your header if your other two classes are working fine. It's presuming and expecting an int, so that seems the case.
Make sure #include<string> is in your header

"undeclared identifier" is actually declared

I've been getting error C2065s for variables that I've declared in the class header file as public data members, one int and one pointer to that int. The lines of code being flagged as erroneous are only when I use these variables in a function - within the class' constructor, they appear to get through okay.
I'm using Visual Studio 2010 Express to write normal C++ (not Visual C++), and here's the output of the compiler's error log:
1>------ Build started: Project: Project 2, Configuration: Debug Win32 ------
1> BaseClassWithPointer.cpp
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(27): error C2065: 'q' : undeclared identifier
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(27): error C2541: 'delete' : cannot delete objects that are not pointers
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(32): error C2065: 'num' : undeclared identifier
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(33): error C2065: 'q' : undeclared identifier
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(34): error C2065: 'q' : undeclared identifier
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Finally, here're my code blocks and headers:
BaseClassWithPointer.h
#pragma once
#include <iostream>
using namespace std;
class BaseClassWithPointer
{
public:
int num;
int *q;
BaseClassWithPointer();
BaseClassWithPointer(int value);
BaseClassWithPointer(const BaseClassWithPointer& otherObject);
void destroyPointer();
virtual void print();
virtual ~BaseClassWithPointer(); //Destructor is virtual so that derived classes use their own version of the destructor. ~ (2. Inheritance - base class with pointer variables – destructors.)
const BaseClassWithPointer& operator= (const BaseClassWithPointer &rhs); //Assignment operator is overloaded to avoid shallow copies of pointers. ~ (3. Inheritance – base class with pointer variables – assignment operator overloading.)
};
BaseClassWithPointer.cpp
#pragma once
#include "BaseClassWithPointer.h"
#include <iostream>
using namespace std;
BaseClassWithPointer::BaseClassWithPointer()
{
num = 0;
q = &num;
}
BaseClassWithPointer::BaseClassWithPointer(int value)
{
num = value;
q = &num;
}
BaseClassWithPointer::BaseClassWithPointer(const BaseClassWithPointer& otherObject)
{
num = otherObject.num;
q = &num;
}
void destroyPointer()
{
delete q;
}
void print()
{
cout << "Num: " << num << endl;
cout << "Value pointed to by q: " << *q << endl;
cout << "Address of q: " << q << endl;
}
BaseClassWithPointer::~BaseClassWithPointer()
{
destroyPointer();
}
const BaseClassWithPointer& BaseClassWithPointer::operator=(const BaseClassWithPointer &rhs)
{
if (this != &rhs)
{
num = rhs.num;
q = &num;
}
return *this;
}
You forgot the Class identifier for your destroyPointer() method.
Try
void BaseClassWithPointer::destroyPointer()
instead
This:
void destroyPointer()
...
void print()
Should be
void BaseClassWithPointer::destroyPointer()
{
....
}
void BaseClassWithPointer::print()
{
....
}
etc.
The function destroyPointer() is not part of the class in the cpp file.
It should be:
void BaseClassWithPointer::destroyPointer()
{
delete q;
}
but is:
void destroyPointer()
{
delete q;
}
This is why it cannot find q