C++ Compilation error: Undefined identifier (for a function parameter) - c++

I have a C++ class main.cpp in which I created a class like following:
class MapSearchNode
{
public:
unsigned int x; // the (x,y) positions of the node
unsigned int y;
MapSearchNode() { x = y = 0; }
MapSearchNode( unsigned int px, unsigned int py ) { x=px; y=py; }
float goalDistance( MapSearchNode &nodeGoal );
};
float MapSearchNode::goalDistance( MapSearchNode &nodeGoal )
{
float xd = fabs(float(((float)x - (float)nodeGoal.x)));
float yd = fabs(float(((float)y - (float)nodeGoal.y)));
return xd + yd;
}
int main{
//treatment
}
And it works fine but then I wanted to seperate the class MyClass, so I created a MyClass.h and MyClass.cpp and seperated the code like following:
MyClass.h
#ifndef _MAPSEARCHNODE_H
#define _MAPSEARCHNODE_H
class MapSearchNode
{
public:
MapSearchNode();
MapSearchNode( unsigned int px, unsigned int py );
public:
unsigned int x;
unsigned int y;
float goalDistance( MapSearchNode &goalNode );
};
#endif
MyClass.cpp
#include "MapSearchNode.h"
MapSearchNode::MapSearchNode():x(0), y(0))
{}
MapSearchNode::MapSearchNode( unsigned int px, unsigned int py ):x(px), y(py)
{}
float MapSearchNode::goalDistance(MapSearchNode &goalNode ){
float xDistance = fabs(float(((float)x - (float)goalNode.x)));
float yDistance = fabs(float(((float)y - (float)goalNode.y)));
return xDistance + yDistance;
}
Bur when i try to compile i have an error:
Undefined identifier goalNode;
//for the function goalDistance
Can someone please explain me why am I getting this error and how to fix it.
EDIT: I hope I haven't forgotten anything now.
EDIT:
Well thanks a lot to those who downrated. Some of us are not experts like you and it's hard for them to see errors even small ones!

On the first look there are 2 possible reasons for this error:
The most obvious one:
Your class definition is incomplete, it should be
class MyClass
{
public:
unsigned int x;
unsigned int y;
MyClass() { x = y = 0; }
MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }
float Calculate( MyClass &myClass );
}; // semicolon
float MyClass::Calculate( MyClass &myClass )
{
if(x<myClass.x)
....//treatment
}
Additionally, make sure your source file (.cpp) includes your header file (.h)
So both files would look like that:
// .h file
class MyClass
{
public:
unsigned int x;
unsigned int y;
MyClass() { x = y = 0; }
MyClass( unsigned int px, unsigned int py ) { x=px; y=py; }
float Calculate( MyClass &myClass );
};
// .cpp file
#include "MyClass.h"
//definition of constructors
float MyClass::Calculate( MyClass &myClass )
{
if(x<myClass.x)
....//treatment
}

Related

c++ error: :-1: error: symbol(s) not found for architecture x86_64 - in Qt-Creator

i'm working on an exercise at uni and every time i try to compile the main.cpp i got always the same error.
actor.h:
class Actor {
public:
Actor();
Actor(double x0, double y0);
void move();
double pos_x();
double pos_y();
static const int ARENA_W = 500;
static const int ARENA_H = 500;
};
plane.h (subclass of actor):
class Plane:Actor
{
public:
Plane();
Plane(double x0, double y0);
void move();
double pos_x();
double pos_y();
//int dx = 5;
static const int W = 50;
static const int H = 20;
private:
double x, y;
};
plane.cpp
#include "plane.h"
#include "actor.h"
Plane::Plane(double x0, double y0)
{
this ->x = x0;
this ->y = y0;
//this -> dx;
}
void Plane::move()
{
x = x + 2.5 ;
}
double Plane::pos_x()
{
return x;
}
double Plane::pos_y()
{
return y;
}
main.cpp
include "plane.h"
include"actor.h"
using namespace std;
int main(int argc, char *argv[])
{
Plane plane1(25.0, 5.0);
plane1.move();
double x = plane1.pos_x();
double y = plane1.pos_y();
cout << x << " , " << y<<endl;
}
i saw there are many questions about this problem but i didn't fix it.
can you help me please()?
thank you
You've declared a class Actor in actor.h:
class Actor {
public: Actor();
};
This means that you're going to write some code that will define this construction. This would typically end up in an Actor.cpp file.
If you attempt to construct an Actor without having this implementation, you will get an error from the linker because you're missing the default constructor.
Now you've declared a Plane that's a subclass of an Actor:
class Plane : Actor {
};
and you've defined a non-default constructor:
Plane::Plane(double, double) {
// does something
}
As Plane is a subclass of Actor, there's an implicit construction of a default Actor as part of the construction of Plane, and as you declared that there would be an implementation, the linker is expecting it. As you never defined it in the code, the linker fails at this point.
The somewhat simplistic solution is to add a trivial constructor in actor.h; namely:
class Actor {
public:
Actor() {} // replace the ; with {}
Actor(double x0, double y0);
void move();
double pos_x();
double pos_y();
static const int ARENA_W = 500;
static const int ARENA_H = 500;
};
Now, as for behaviours here - none of the move, pos_x or pos_y methods are declared virtual, so they're not being overloaded in Plane; they're simply being replaced. This may come up later in your course.

Way around "first defined here" error?

I need to have two alternate classes with the same name, that I can switch between each other by simply changing which class is included in main.
For example;
Mode_1.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
Mode_2.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
main.cpp
#include "Mode_1.h"
int main(){
Draw D;
int x = 2;
char y = 'x';
D.Render(x, y);
}
Currently I'm having to comment out the .h and .cpp files I'm not using to avoid the "first defined here" error. What I want is that all I have to do to switch between them is change
#include "Mode_1.h"
to
#include "Mode_2.h"
You should put them in different namespaces:
namespace Mode2
{
class Draw{
private:
// private stuff
public:
Draw(int x, char y);
};
}
In main you can then select the namespace you want to use:
#include "Mode_1.h"
#include "Mode_2.h"
using namespace Mode2;
int main()
{
Draw D;
int x = 2;
char y = 'x';
D.Draw(x, y);
return 0;
}
You may try like this:
#ifdef MODE1
#include "Mode_1.h"
#else
#include "Mode_2.h"
#endif
int main(){
Draw D;
int x = 2;
char y = 'x';
Draw(x, y);
}
And compile this source file with -DMODE1 or none depending on you wish to include Mode_1.h or Mode_2.h

C++ inherited functions not being found

I new in C++ and I have difficulty to understand how to get my function with inheritance.
I have a Class that is link to another with inheritance, everything work except:
I cannot reach my superclass function.
Here's my class header : Point.h (I don't include the .cpp):
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int , int);
void set_values (int , int , int );
void affichervaleurs();
int getX() const { return x; }
int getY() const { return y; }
private:
int x ;
int y ;
int z ;
};
#endif
Now My other class that try to access the function getX from Point.h :
The header : Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre : public Point{
public:
Carre();
//Carre(int a , int b);
//Carre(int a, int b):Point(a,b) {};
//Carre(int a, int b, int c):Point(a, b, c) {};
//const Point &pp;
int Aire (){
};
void affichercar(){
};
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
#include "Carre.h"
#include "Point.h"
Carre::Carre():Point(){
};
//Carre::Carre(int a, int b);
//const &pp;
int Aire (){
return (getX() * getY());
};
void affichercar(){
//cout << "Coordonnees X:" << x << endl;
};
It says that my GetX() is undeclared in my Carre.cpp .
Like I said I'm new in C++
Does someone know what I'm missing to make that code work. ?
Your definition is missing the class scope, which makes it a free function instead of a member.
It should be
int Carre::Aire (){
return getX() * getY();
};
In the .cpp file for Carre, the functions Aire and affichercar are global. Presumably you intended:
int Carre::Aire(){
return (getX() * getY());
};
For example.
Declaring function outside class body requires a class specifier:
int Carre::Aire () {
return (getX() * getY());
};
void Carre::affichercar() {
//...
}
Otherwise
int Aire () {
return (getX() * getY());
};
is just another function in global namespace that can exists simutaneously to Carre::Aire().
This is because you are not implementing the Aire function as being part of the Carre class.
Try changing
int Aire (){
to
int Carre::Aire (){
Also, you already have an implementation of the Aire method in the header file. You should either implement the function inline in the header file, or in the .cpp file, but not both. This also applies to your affichercar method.

C++ 'class' type redefinition

I have been attempting to work with classes in c++ for the first time. My circle class and associated header file were working fine, I then moved some files and since then keep getting an error which i have displayed below.
c:\circleobje.cpp(3): error C2011: 'CircleObje' : 'class' type redefinition
c:\circleobje.h(4) : see declaration of 'CircleObje'
CircleObje.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
};
#endif
CircleObje.cpp
#include "CircleObje.h"
class CircleObje {
float rVal, gVal, bVal;
int xCor, yCor;
public:
void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
};
I haven't copied all of the .cpp functions as I didn't think they were relevant. These files were working without issue before I moved the file locations. Even after renaming them I still have the same error as above. Any ideas to solve the problem?
The issue is that you are defining the class twice just as the compiler is telling you. In the cpp you should provide the definitions of the functions like so:
MyClass::MyClass() {
//my constructor
}
or
void MyClass::foo() {
//foos implementation
}
so your cpp should look like:
void CirleObje::setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
And all the class variables should be defined in the .h file inside of your class.
You have defined the class twice, in the header and in the cpp, so in the .cpp the compiler sees two definitions. Remove the definition of the class on the .cpp.
Class functions should be implemented in the cpp in this way:
<return_type> <class_name>::<function_name>(<function_parameters>)
{
...
}
Consider this example class:
//foo.hpp
struct foo
{
int a;
void f();
}
The class is implemented in the foo.cpp file:
#include "foo.hpp"
void foo::f()
{
//Do something...
}
you are declaring your class multiple times once in header file and another in .cpp file which is redefining your class.
CircleObje.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
public:
float rVal, gVal, bVal;
int xCor, yCor;
};
#endif
CircleObje.cpp
#include "CircleObje.h"
void CircleObje::void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
Remove class CircleObje {, public and the ending bracket }; and it should work. You already defined your class in the .H, thus no need to redefine it in the CPP.
Also, you should write your member implementation (in CPP file) like this :
float CircleObje::getR() { /* your code */ }
you need to put #pragma once in first line of header file then the errors will be disappears

Error: "undefined reference to 'function'" in C++

I got an error while compiling C++:
/tmp/ccqs6UN2.o: In function main': PowerModulus.cpp:(.text+0x194): undefined reference to takeModulusLOOP(int, int, int)' collect2: ld returned 1 exit status
The source code:
#include "PowerModulus.h"
#include <iostream>
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
int main() {
std::cout << takeModulusLOOP(5348, 700, 335);
}
int PowerModulus::takeModulusLOOP(int x, int n, int moduint) {
int total = modint(x, moduint);
n--;
while (--n) {
total = modint(total * x, moduint);
}
return total;
}
int PowerModulus::modint(int x, int moduint) {
while (x < 0) // Deal with negative
x += moduint;
return x % moduint; // Comes out positive now -> %
}
PowerModulus::PowerModulus() {
// TODO Auto-generated constructor stub
}
PowerModulus::~PowerModulus() {
// TODO Auto-generated destructor stub
}
Header file:
#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
class PowerModulus {
public:
int takeModulusLOOP(int x, int n, int moduint);
int modint(int x, int moduint);
PowerModulus();
virtual ~PowerModulus();
};
#endif /* POWERMODULUS_H_ */
Where is the error?
You have declared a global takeModulusLOOP function, then call it in main, without ever defining it. This is a different function than PowerModulus::takeModulusLOOP.
// main.cpp
#include "PowerModulus.h"
#include <iostream>
int main(){
std::cout << PowerModulus::takeModulusLOOP(5348,700,335) << '\n';
return 0;
}
Changed to a namespace instead of a class, and separated into header and implementation (instead of grouping in main.cpp):
// PowerModulus.cpp
#include "PowerModulus.h"
namespace PowerModulus {
int takeModulusLOOP(int x, int n, int moduint){
int total = modint(x, moduint) ;
n--;
while (--n){
total = modint( total * x, moduint );
}
return total;
}
int modint(int x, int moduint){
while ( x < 0) // deal with negative
x += moduint;
return x % moduint;//comes out positive now -> %
}
}
Header:
// PowerModulus.h
#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_
namespace PowerModulus {
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
}
#endif
This line:
std::cout << takeModulusLOOP(5348,700,335);
is calling the non-class takeModulusLOOP, which you haven't defined anywhere.
You should either call the class version, by providing an object of the class type and using something like:
PowerModulus p;
std::cout << p.takeModulusLOOP(5348,700,335);
(most likely) or providing a non-class version (least likely).
You could also consider making the function static since it doesn't seem to require an object at all. Then you don't need to instantiate one.
You receive the error, because you do not have such a function.
Actually, you have it in PowerModulus class, so you should call the function from PowerModulus instance.
PowerModulus pM;
pM.takeModulusLoop(5348,700,335);
You do not need to claim the function in the beginning of your .h file or in the beginning of your .cpp file.
If you intended to use the takeModulusLoop function of the PowerModulus class then you need not declare a global function again...
But, if you intended to use a different global function, then you need to define it in its context...