i am a beginner and i have already read a doc but i need to practice for learn and now i am stuck.
So i would like to do a class takeDommage for apply a number of dmg and activate a countdown for create the invincibility frame.
so i tryed this (see code under)
It's the first class i create alone so i don't understand what's wrong
main :
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
std::cout<<"collision pp"<<std::endl;
takeDommage::prendreDegat(10);
std::cout<<pv<<std::endl;
}
takeDommage.h :
#ifndef TAKEDOMMAGE_H
#define TAKEDOMMAGE_H
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
class takeDommage
{
public:
takeDommage();
prendreDegat(int Dommage);
//virtual ~takeDommage();
protected:
sf::Clock takeDammageClock;
int Dommage;
private:
};
#endif // TAKEDOMMAGE_H
takeDommage.cpp
#include "takeDommage.h"
takeDommage::takeDommage()
{
}
void takeDommage::prendreDegat(int Dommage)
{
if(takeDammageClock.getElapsedTime().asSeconds()>=3)
{
std::cout << "bite" << std::endl;
pv -= Dommage;
takeDammageClock.restart();
}
}
error:
||=== Build: Debug in TheGameSFML (compiler: GNU GCC Compiler) ===|
E:\Work\Top_secret\code\TheGame\main.cpp||In function 'int main()':|
E:\Work\Top_secret\code\TheGame\main.cpp|168|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
E:\Work\Top_secret\code\TheGame\main.cpp|180|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
E:\Work\Top_secret\code\TheGame\main.cpp|217|error: cannot call member function 'int takeDommage::prendreDegat(int)' without object|
E:\Work\Top_secret\code\TheGame\main.cpp|156|warning: unused variable 'enemySpawnTimer' [-Wunused-variable]|
E:\Work\Top_secret\code\TheGame\src\takeDommage.cpp|8|error: prototype for 'void takeDommage::prendreDegat(int)' does not match any in class 'takeDommage'|
include\takeDommage.h|15|error: candidate is: int takeDommage::prendreDegat(int)|
||=== Build failed: 3 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
you seem to be very new in c++. the compiler already telling you what's wrong with it.
error: cannot call member function 'int takeDommage::prendreDegat(int)' without object|
you need to instantiate (create) your object first. the way you accessing as if the takeDommage function is a static function which is not. its public a member function of takeDamage
assuming that you have instantiated your takeDommage class somewhere before the if statement call,
TakeDommage Obj;
...
...
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
std::cout<<"collision pp"<<std::endl;
Obj.prendreDegat(10); //calling the prendredegat member function of Obj
std::cout<<pv<<std::endl;
}
in addition to that you are missing the return type void before the function name of prendreDegat
class takeDommage
{
public:
takeDommage();
//prendreDegat(int Dommage); //missing void
void prendreDegat(int Dommage); //correct way. which can be translated as Prendredegat returns nothing.
Related
container.hpp
#ifndef CONTAINER_HPP
#define CONTAINER_HPP
#include <functional>
namespace lasd {
/* ************************************************************************** */
class Container {
private:
// ...
protected:
unsigned long size = 0;
public:
// Destructor
virtual ~Container() = default;
/* ************************************************************************ */
// Copy assignment
Container& operator=(const Container&) = delete; // Not usable.
// Move assignment
Container& operator=(Container&) = delete; // Not usable.
/* ************************************************************************ */
// Comparison operators
bool operator==(const Container&) const noexcept = delete; // Not usable.
bool operator!=(const Container&) const noexcept = delete; // Not usable.
/* ************************************************************************ */
// Specific member functions
virtual inline bool Empty() const noexcept;
virtual inline unsigned long Size() const noexcept;
virtual void Clear() = 0;
};
#include "container.cpp"
}
#endif
container.cpp
// Specific member functions (Container)
inline unsigned long Container::Size() const noexcept{
return size;
}
inline bool Container::Empty() const noexcept{
return size == 0;
}
Give this in output. The funniest part is that all this code is provided by my professor, and I only have coded the .cpp file. I've already tried to add #include "container.hpp" to my .cpp file.
||=== Build: Debug in Exercise1 (compiler: GNU GCC Compiler) ===|
||=== Build: Debug in Exercise1 (compiler: GNU GCC Compiler) ===|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|4|error: 'Container' has not been declared|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|4|error: non-member function 'long unsigned int Size()' cannot have cv-qualifier|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp||In function 'long unsigned int Size()':|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|5|error: 'size' was not declared in this scope|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|8|error: 'Container' has not been declared|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|8|error: non-member function 'bool Empty()' cannot have cv-qualifier|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp||In function 'bool Empty()':|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|9|error: 'size' was not declared in this scope|
C:\Users\Giulia\Desktop\Università\Laboratorio ASD\exercise1\container\container.cpp|38|error: expected initializer before '<' token|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
During professor video lectures, he has compiled the code and it worked so well. I really cannot understand why, by using Code::Blocks, it gives me this error (I've tried on the gpp-compiler on Atom editor too, with the exactly same result).
Thanks.
Most likely, in your project you are compiling container.cpp separately, as any reasonable project would do. However, your professor did something that would never pass any code review - he included a cpp file in a header file.
You need to change your project to not compile container.cpp separately (or better yet, get rid of that include and fix container.cpp to make it possible to compile it).
Note the namespace lasd in your header file. You should either explicitly namespace every function in your cpp file (lasd::Container::Size) or also wrap it in a namespace lasd { ... } block.
I am pursuing some interest in c++ programming by way of self instruction. I am working on some basic stuff for now and am currently having issue getting my classes talking/instantiated?.
I am trying to get my main cpp file to compile alongside a header and call to some class functions through the main using a more efficient command method.
I am stuck and would appreciate some help. I will include both files. I am just trying to get a return value from the header by calling the function.
error:
main.cpp:6.21 error: cannot call member function 'void myClass::setNumber(int) without object
the code works when compiled with the main, so it is something with the 'scope resolution operator' i think. First is main.cpp
#include <iostream>
#include "myClass.h"
using namespace std;
int main(){
myClass::setNumber(6);
{
return number;
}
}
Then my header file myClass.h
// MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
class myClass {
private:
int number;//declares the int 'number'
float numberFloat;//declares the float 'numberFloat
public:
void setNumber(int x) {
number = x;//wraps the argument "x" as "number"
}
void setNumberFloat(float x) {
numberFloat = x;
}
int getNumber() {//defines the function within the class.
number += 500;
return number;
}
float getNumberFloat() {//defines the function
numberFloat *= 1.07;
return numberFloat;
}
};
#endif
Any help?
The error message says everything:
cannot call member function 'void myClass::setNumber(int)' without object
You need to create an object first:
myClass obj;
then call the class method on that object:
obj.setNumber(6);
The value 6 will get assigned to the number field of the obj variable.
So the title is somewhat misleading. But its exactly what I am trying to do. I created a small case scenario. This case works in Visual Studio but when trying it on Mingw I get an error. Here is the case. I am trying to call a function inside a cpp file from a static method which resides in a different cpp file. This is just rough code which will get my point across.
File:foo.h
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED
#include <iostream>
struct foo
{
int someMethod();
};
#endif // FOO_H_INCLUDED
File: foo.cpp
#include "foo.h"
int someFunction()
{
std::cout << "SomeFunction";
return 0;
}
int foo::someMethod()
{
std::cout << "foo called";
return 0;
}
File:main.cpp
void myfunction()
{
}
struct bar
{
static void somebar()
{
someFunction(); //error: 'someFunction' was not declared in this scope
myfunction(); //OK
}
};
int main()
{
}
My question is why am I getting an error on someFunction();
This is my compiler output
g++.exe -Wall -std=c++98 -g -std=c++11 -I..\..\..\mingw64\include -c C:\Users\peeru\TestCodeBlocks\foo.cpp -o obj\Debug\foo.o
C:\Users\peeru\TestCodeBlocks\foo.cpp: In function 'int someFunction()':
C:\Users\peeru\TestCodeBlocks\foo.cpp:6:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
C:\Users\peeru\TestCodeBlocks\foo.cpp: In member function 'int foo::someMethod()':
C:\Users\peeru\TestCodeBlocks\foo.cpp:11:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
g++.exe -Wall -std=c++98 -g -std=c++11 -I..\..\..\mingw64\include -c C:\Users\peeru\TestCodeBlocks\main.cpp -o obj\Debug\main.o
C:\Users\peeru\TestCodeBlocks\main.cpp: In static member function 'static void bar::somebar()':
C:\Users\peeru\TestCodeBlocks\main.cpp:14:21: error: 'someFunction' was not declared in this scope
someFunction();
^
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 2 warning(s) (0 minute(s), 0 second(s))
Any suggestions ?
As the compiler says, someFunction hasn't been declared in main.cpp, only in a separate translation unit, foo.cpp. Functions need to be declared before use.
Add a declaration in either main.cpp, or a header included from both:
int someFunction();
You also need to return something from the functions that claim to return int, as the other warnings say.
You have to provide a declaration for the function in main.cpp too, modify the foo.h header accordingly:
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED
#include <iostream>
int someFunction();
struct foo
{
int someMethod();
};
#endif // FOO_H_INCLUDED
and add #include "foo.h" in main.cpp.
I am not sure why MSVC++ compiled without complaining, though.
Method someFunction and all others are declared as returning type int. So, add return 0 at the end of functions or declare them as void. And you cannot call non static function from static function.
You declared someFunction() in foo.cpp, but not in main.cpp. When main.cpp is compiling, it doesn't know the declaration of someFunction(), so it fails.
You need to:
add int someFunction(); to the top of main.cpp
add int someFunction(); to the foo.h file, and then #include "foo.h" in main.cpp
I'm sure that the answer is staring me straight in the face, but I haven't been able to make any progress due to this... First some code:
objects/testObject.h:
#include <irrlicht.h>
#include "../maths.h"
using namespace irr;
#ifndef testObject_H
#define testObject_H
class testObject : public scene::SAnimatedMesh
{
public:
testObject(IrrlichtDevice* device);
virtual ~testObject();
protected:
const char* meshInfoLocation;
int totAnims;
private:
};
#endif
objects/testObject.cpp:
#include "testObject.h"
testObject::testObject(IrrlichtDevice* device) : scene::SAnimatedMesh()
{
io::IrrXMLReader* modelInformation = io::createIrrXMLReader(meshInfoLocation);
while(modelInformation->read())
{
if(modelInformation->getNodeName() == "totAnims") totAnims = stringToInt(modelInformation->getAttributeValue("totAnims"));
}
}
testObject::~testObject() { } //Incomplete, but should still compile...
When I compile this code, I get the following errors:
/home/david/workspace/spaceSim/objects/testObject.cpp||In constructor ‘testObject::testObject(irr::IrrlichtDevice*)’:|
/home/david/workspace/spaceSim/objects/testObject.cpp|20|warning: comparison with string literal results in unspecified behaviour [-Waddress]|
/home/david/workspace/spaceSim/main.cpp||In function ‘int main(int, char**)’:|
/home/david/workspace/spaceSim/main.cpp|24|warning: ‘virtual bool irr::io::IFileSystem::addZipFileArchive(const c8*, bool, bool)’ is deprecated (declared at /home/david/irrlicht-1.8.1/include/IFileSystem.h:228) [-Wdeprecated-declarations]|
/home/david/workspace/spaceSim/objects/testObject.cpp||In constructor ‘testObject::testObject(irr::IrrlichtDevice*)’:|
/home/david/workspace/spaceSim/objects/testObject.cpp|20|warning: comparison with string literal results in unspecified behaviour [-Waddress]|
obj/Debug/objects/testObject.o||In function `testObject::testObject(irr::IrrlichtDevice*)':|
/home/david/workspace/spaceSim/objects/testObject.cpp|3|multiple definition of `testObject::testObject(irr::IrrlichtDevice*)'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|3|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::testObject(irr::IrrlichtDevice*)':|
/home/david/workspace/spaceSim/objects/testObject.cpp|3|multiple definition of `testObject::testObject(irr::IrrlichtDevice*)'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|3|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|27|multiple definition of `testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|27|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|27|multiple definition of `testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|27|first defined here|
obj/Debug/objects/testObject.o||In function `virtual thunk to testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|29|multiple definition of `virtual thunk to testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|29|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|27|multiple definition of `testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|27|first defined here|
obj/Debug/objects/testObject.o||In function `virtual thunk to testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|29|multiple definition of `virtual thunk to testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|29|first defined here|
||=== Build finished: 14 errors, 3 warnings ===|
I've tried the following at solving:
Combining the header and the cpp files.
Emptying out all the method bodies and removing the #includes so that all that matters is the class structure.
Google searches (without any luck...)
Thank you for the help!
I compiled your code using gcc4.8.1 from mingw32 (www.mingw.org) (putting them into file, and replacing missing types). The compilation seems to be OK. I guess the problem could be
#include <irrlicht.h>
#include "../maths.h"
code:
//#include <irrlicht.h>
//#include "../maths.h"
//using namespace irr;
#ifndef testObject_H
#define testObject_H
#include <tuple>
namespace scene {
typedef std::tuple<int,int> SAnimatedMesh;
};
typedef int IrrlichtDevice;
class testObject : public scene::SAnimatedMesh
{
public:
testObject(IrrlichtDevice* device);
virtual ~testObject();
protected:
const char* meshInfoLocation;
int totAnims;
private:
};
#endif
//#include "testObject.h"
testObject::testObject(IrrlichtDevice* device) : scene::SAnimatedMesh()
{
/*
io::IrrXMLReader* modelInformation = io::createIrrXMLReader(meshInfoLocation);
while(modelInformation->read())
{
if(modelInformation->getNodeName() == "totAnims") totAnims = stringToInt(modelInformation->getAttributeValue("totAnims"));
}
*/
}
testObject::~testObject() { } //Incomplete, but should still compile...
int main() {}
I have this problem with signal():
This code compiles fine:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void terminate( int param )
{
printf ( "Terminating program...\n" );
exit( 1 );
}
int main()
{
signal( SIGTERM, terminate );
return 0;
}
The following code, however, throws this error:
g++ -Wall -c -g goober.cpp
goober.cpp: In member function `void GOOBER::yarrgh()':
goober.cpp:5: error: argument of type `void (GOOBER::)(int)' does not match `
void (*)(int)'
make: *** [goober.o] Error 1
goober.h:
#ifndef GOOBER_H
#define GOOBER_H
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
using namespace std;
class GOOBER {
public:
GOOBER(){}
~GOOBER(){}
void yarrgh();
void terminate( int param );
};
#endif
goober.cpp:
#include "goober.h"
void GOOBER::yarrgh()
{
signal( SIGTERM, terminate );
}
void GOOBER::terminate( int param )
{
printf( "Terminating program...\n" );
exit( 1 );
}
driver.cpp:
#include "goober.h"
using namespace std;
int main()
{
GOOBER G;
G.yarrgh();
return 0;
}
I don't see any difference in the code, other than I'm calling signal() in a member. Any ideas what's wrong, and how to fix it?
You need to declare your terminate() function as static:
class GOOBER {
// ...
static void terminate(int param);
};
This is because that as a non-static member function, the terminate() function expects to be passed the (hidden) this parameter to point to some instance of the object. Since the signal mechanism doesn't know about this (or about anything much of C++), you need to use a static function so that there is no hidden this parameter.
I can tell you what's wrong:
You cannot use a non-static member function like a normal function pointer. Member functions always have an implicit this argument, which is provided (implicitly) by the caller. A C API cannot do this.
Terminate has to be a static function in the class.
Member function have different signature from normal function that not belong any class. However, static member function have the same signature with normal function. So, you could declare your terminate member function in GOOBER class as static.