"No matching constructor for initialization of. . ." - c++

I recognize that this type of question has been asked, and I looked at those responses but still think I'm missing something. I get this "No matching constructor error", because I don't have a constructor, but that being said, everything that I looked at about constructors said that you need them if you don't already include the variable names inside the class. But I already did that, so do I need a constructor? If I do, what should it look like then? I'm new to C++, taking a class, and this is for an assignment.
Here's my sensor_node.h file with the class declaration:
#ifndef SENSORNODE_H
#define SENSORNODE_H
#include <iostream>
class LOCATION {
float lat, longi, height;
public:
LOCATION (float lat, float longi, float height);
void setx(float xx);
void sety(float yy);
void setz(float zz);
void print();
};
class SensorNode {
char* NodeName;
int NodeID;
LOCATION Node1;
float batt;
int func;
public:
SensorNode(char *n, float x, float y, float z, int i, float ah);
void print();
void setOK(int o);
int getOK();
void setLOC(float longi, float lat, float h);
};
#endif /* defined(__Project_3__sensor_node__) */
And here's my main.cpp with the error (On the line that says "LOCATION"):
#include <iostream>
using namespace std;
#include "sensor_node.h"
int main() {
LOCATION a; SensorNode s1("Pulse",15.9,-30.1,0,157,2.0);
int hold;

Actually, you so have a constructor: LOCATION (float lat, float longi, float height). Since it is the only one, C++ tries to apply it. However, you did not provide any parameters, thus this constructor does not match.

You have a constructor for LOCATION (why the inconsistent capitalisation, incidentally?) that takes three floats, but the line LOCATION a tries to call the default constructor, which you haven't defined.

Related

C++: Function call error: identifier "name" is undefined, when already defined?

I'm having problems with several C++ programs simply not wanting to run the functions that are defined clearly in a public class above main. I've looked far and wide for answers, but similar problems are the result of not having a scope resolution operator or something similar. As far as I can tell, everything required to call this function is there.
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
class Box{
public:
Box();
Box(int x, int y);
Box(int x, int y, char type);
Box(char type);
//Accessor functions:
int GetY();
int GetX();
char GetChar();
//Mutator functions:
void SetCoords(int x, int y);
void SetChar(char x);
//Output function:
void printbox(void);
private:
int ycoord;
int xcoord;
char drawing;
};
int main(int argc, char* argv[])
{
Box();
printbox();
return 0;
};
void Box::printbox(void){
//working code
};
What I get instead is error C3861: 'printbox' identifier not found. What's missing that lets the printbox (and other functions like these) run?
printbox is a method therefore you have to call it on an object of type Box. Like this
Box b;
b.printbox();

declaration does not declare anything [-fpermissive] error

I get "decleration does not declare anything [-fpermissive] error";
Here is my code;
#ifndef CAMERA_H
#define CAMERA_H
#include "Vector.h"
#include <string>
using namespace std;
class Camera
{
private:
int id;
float position[3];
Vector gaze;
Vector up;
float left;
float right;
float bottom;
float top;
float near;
float far;
int type;
public:
Camera(int i,string c, string g, string u, string f, string t);
int getID() {return id; }
float* getPosition() { return position; }
Vector getGaze() { return gaze; }
Vector getUp() { return up; }
float getLeft() {return left;}
float getRight() {return right;}
float getBottom() {return bottom;}
float getTop() {return top;}
float getNear() {return near;}
float getFar() {return far;}
int getType() {return type;}
};
#endif // CAMERA_H
the error starts at "float near;" and continues next 3 lines.
What is the reason of this error and how can I fix it.
Replace near and far with something else, at least for a test, e.g. near_ and far_: I suspect you are compiling with some funny header which defines near and far to be nothing. In the distant past these two were keywords used on some platforms to deal with pointers of different sizes.
If you want to verify the theory, process the source file with the -E option (you may need to remove other options from the compile line, e.g., -c): with this option the compiler produces the preprocessed output. If you capture that and look at your class there I'm quite certain that it won't contain the member names.
I suspect "near" and "far" are reserved names somewhere in "Vector.h" or string.
Renaming these variables something different should fix your problem.

Trouble passing an object as a parameter

Hey I'm trying to pass a class object that I have made into another class to read that data. The error I'm getting is c2061: syntax error: identifier 'Player'
This is my Player2.h
#pragma once
#include "DarkGDK.h"
#include "Input.h"
#include "Player.h"
class Player2{
public:
Player2();
void PlayerSetup();
void PlayerUpdate(Player& user1);
void PlayerHealthReset();
void Gravity();
float GetPosX();
bool CheckMatchEnd();
void PlayerFire(Player& user1);
void PlayerCheckHitEnemies(Player& user1);
private:
float Vx;
float Vy;
float PosX;
float PosY;
float Speed;
int Lives;
int Health;
//
int gravity;
bool playerJumping;
bool matchEnd;
bool playerIsFiring;
float playerBullet;
bool directionBullet;
};
And the error I'm getting is that It can't recognize Player even though I brought in the Player header.
Here is Player.h
class Player{
public:
Player();
void PlayerSetup();
void PlayerUpdate(float PosX2);
void PlayerHealthReset();
float GetPosX();
float GetPosY();
void Gravity();
bool CheckMatchEnd();
void PlayerFire(float PosX2);
private:
float Vx;
float Vy;
float PosX;
float PosY;
float Speed;
int Lives;
int Health;
float playerBullet;
bool playerIsFiring;
int gravity;
bool playerJumping;
bool matchEnd;
bool directionBullet;
};
All the respective code within the header file works 100%, as I've tested it.
player does not compile before player2 is defined, so placing class player above your player2's declaration will compile player BEFORE moving onto player 2.
class player;
class player2{
//...
};
-Also as Hunter McMillen suggested think about making player 2 inherit from a base class, maybe player that defines standard methods all players would use(I dont want to steal hunter's idea, i'll let him post answer about this if he pleases with a more in depth approach).

Simple reference variable assignment causing segfault in global pointer to object?

I'm getting a segfault on line 15 of my .cpp file. I'm not sure why. Various code snippets:
explosionhandler.h:
class explosionhandler {
public:
struct explosion {
...
};
vector<struct explosion> explosions;
struct explosion_type {
...
};
vector<struct explosion_type> type;
int num_types;
explosionhandler();
~explosionhandler();
void registerexplosion(int& ttype,ALLEGRO_BITMAP*& b,int seq, float a, float m,float e);
void createexplosion(int ttype,float x,float y);
void drawexplosions(ALLEGRO_BITMAP* screen);
void gettype(explosion_type& a,ALLEGRO_BITMAP*& b,int& nseq, float& aa, float& ee, float& mm);
};#endif
explosionhandler.cpp:
explosionhandler::explosionhandler()
{
num_types=0;
}
void explosionhandler::registerexplosion(int& ttype,ALLEGRO_BITMAP*& b,int seq, float a, float m,float e)
{
explosion_type n;
....
ttype = num_types; /*********** right here *******************/
num_types++;
type.push_back(n);
}
explosionhandler passed as pointer to object rocket:
rocket.h:
...
class explosionhandler;
class rocket {
public:
...
void setrocket(ALLEGRO_BITMAP*& a,ALLEGRO_BITMAP*& b, explosionhandler*& h);
...
int exptype;
...
}; #endif
rocket.cpp:
rocket::rocket()
{
...
exptype=-1;
}
void rocket::setrocket(ALLEGRO_BITMAP*& a,ALLEGRO_BITMAP*& b, explosionhandler*& h)
{
handler = h;
area.sethitboundaries(a);
fprintf(stdout,"setrocket, # of rockets in vector: %i\n",(int)rockets.size());
h->registerexplosion(exptype,b,3,(float)al_get_bitmap_width(b),(float)0,(float)-18); //called function
}
and finally main.cpp (abbreviated):
#include "rocket.h"
#include "explosionhandler.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
#include <cstdlib>
#define PI 3.14159265
...
rocket rock(bullet_speed+2,width,height);
explosionhandler *handler;
...
int setup()
{
...
rock.setrocket(rk,exp,handler);
rock.setlimit(5);
al_set_target_bitmap(al_get_backbuffer(display));
...
}
...
Ok Yeah its clear now the problem was handler was not initialized. whoops.
and of course explosionhandler is a pointer in main.cpp, rocket is an object declared in main.cpp, both are globals.
Bless my little noob heart I know not what I do.
My psychic sense tells me that you're calling rocket::setrocket with a NULL pointer as the parameter h (or more specifically, a reference to a NULL pointer), and then you're calling h->registerexplosion() on the NULL pointer.
Don't do that. Pass in a valid pointer instead, or allocate a new object (making sure you delete it properly later).

Inheriting a constructor in a .h and .cpp file

So I've got two classes: Object and Player.
I want Player to inherit from Object because Object has basic functions that I need.
Player has it's added-on functions that expand from what Object can do.
I have four files:
Object.cpp, Object.h, Player.cpp, and Player.h.
To make an example out of my situation, I have added a variable to my Player class:
playerVariable. My Object constructor parameters does not contain this, however my Player constructor does, so you can see that Player expands Object.
Anyways, Here is my code:
Object.h:
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
class Object{
int x, y;
HTEXTURE tex;
hgeAnimation *anim;
float angle, FPS, velocity;
public:
Object(int x, int y, HTEXTURE tex, float FPS);
//Setters
void SetX(int x);
void SetY(int y);
void SetSpeed(int FPS); //Image Speed
void SetAngle(float angle); //Image Angle
void SetVelocity(float velocity); //Object Speed/Velocity
//Getters
};
Object.cpp:
/*
** Object
**
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
Object::Object(int x, int y, HTEXTURE tex, float FPS){
this->x = x;
this->y = y;
this->tex = tex;
this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
this->x = x;
}
void Object::SetY(int y){
this->x = x;
}
void Object::SetSpeed(int FPS){
this->FPS = FPS;
anim->SetSpeed(FPS);
}
void Object::SetAngle(float angle){
this->angle = angle;
}
void Object::SetVelocity(float velocity){
this->velocity = velocity;
}
//Getters
Player.h:
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
class Player : public Object{
int playerVariable;
public:
Player(int x, int y, HTEXTURE tex, float FPS, int playerVariable);
//Setters
void SetX(int x);
void SetY(int y);
void SetSpeed(int FPS); //Image Speed
void SetAngle(float angle); //Image Angle
void SetVelocity(float velocity); //Object Speed/Velocity
//Getters
};
Player.cpp:
/*
** Object
**
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
#include "Player.h"
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable){
this->x = x;
this->y = y;
this->tex = tex;
this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
this->x = x;
}
void Object::SetY(int y){
this->x = x;
}
void Object::SetSpeed(int FPS){
this->FPS = FPS;
anim->SetSpeed(FPS);
}
void Object::SetAngle(float angle){
this->angle = angle;
}
void Object::SetVelocity(float velocity){
this->velocity = velocity;
}
//Getters
My problem is, I'm not exactly sure if I'm setting this up right.
I've looked at tutorials on inheritance but I have no idea how to set it up with both
a .h file and a .cpp file for the classes. Can someone help?
You are defining the Object functionality twice. You don't need to define that, it will be inherited from Object and automatically be available on Player.
Your Player constructor needs to invoke the base Object constructor. This is done with constructor initialization lists:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
: Object(x, y, tex, FPS) // forward arguments to base constructor
{}
You can call the base class constructor this way, specifying every parameter :
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {
It looks like you don't actually need class Player to override SetX(int), SetY(int), SetSpeed(int), SetAngle(float), and SetVelocity(float) of Object. If you did, then you would make these Object member functions virtual.
A few other notes:
You don't need to redefine the Object member functions in Player.cpp. Player is an Object, so it inherits the member functions of class Object. Also, if there are any differences between the implementation in Object.cpp and Player.cpp then you will get linker errors due to a violation of the One Definition Rule.
The Object destructor should be declared virtual.
Rather than setting the members of Object directly in the Player constructor, you could use an initialization list:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
: Object(x, y, tex, FPS), playerVariable(playerVariable)
{
}
Using initialization lists is much more efficient. It is even necessary in this case because Object::x, Object::y, Object::tex, and Object::FPS are all private to Object and friends.
In Object, you should provide a custom copy constructor and copy-assign operator overload (Object::operator=(const Object&)). The C++ compiler provides default implementations of these member functions for you, but because you have pointer members and HTEXTURE members, you likely need to provide explicit implementations to handle the deep copy. The copy constructor and copy-assign overload must always make deep copies.
There is a mismatch on the type of FPS. The Object::FPS member is declared as a float, but the parameter to Object::SetSpeed(int) is an int.