I am just starting an assignment and am beginning with defining the functions of the class in the "Distance.h" tab and although I am pretty sure my functions are initialized correctly, I am still getting compiler errors saying that "definition not found." I used an online tutor and he ran it on his computer and did not get any errors, although he was not any further help with me fixing this issue. Does anyone know what I am supposed to do in this situation as this is my only computer or if anyone can tell me if I actually am just coding wrong.
Here is my "Distance.h":
#pragma once
class Distance
{
private:
long length;
public:
// Transformers
void setLength(long newLength);
void setFeet(int newFeet);
// Observors
long getLength();
int getFeet();
int getInches();
double getLengthInFeet();
};
You have to define your class methods. For instance:
#pragma once
class Distance
{
private:
long length;
public:
//Transformers
void setLength(long newLength){
// TODO: define your method here
// For instance: length = newLength;
}
void setFeet(int newFeet){
// TODO: define your method here
}
//Observors
long getLength(){
// TODO: define your method here
}
int getFeet(){
// TODO: define your method here
}
int getInches(){
// TODO: define your method here
}
double getLengthInFeet(){
// TODO: define your method here
}
};
Related
I am really stuck at this problem for two days now. I know I am missing some crutial information but I don't know what.
I am programming an ESP32 using vscode and platformio.
I have imported ezButton and ezOutput. They both work fine when I call them from main.cpp. I assume this is because I am not calling them from inside a different class. Now I wrote a custom class which does some stuff and I want to read inputs from ezButton and write outputs to ezOutput.
Where my head is stuck at at this point is that I don't now how to correctly initialize the classes correctly.
I imagine it working like in my main.cpp where I for example just type inputPinName.isPressed();and I get my information. This should also work the other way like outputPinName.high();.
I would really appreciate someone explaining this to me or providing a link to a resource that explains this issue.
Here are some code snippets:
main.cpp
#include <ezButton.h>
#include <ezOutput.h>
// INPUTS
ezButton inputPinUp(34, INPUT_PULLDOWN);
//... same for all other inputs
// OUTPUTS
ezOutput outputPinUp(4);
ezOutput outputPinDown(0);
#define DEBOUNCE_TIME 50
void setup()
{
inputPinUp.setDebounceTime(DEBOUNCE_TIME);
//... same for all other pins
}
void loop()
{
inputPinUp.loop();
//... same for all other pins
}
shutter.h
#include <ezButton.h>
#include <ezOutput.h>
class shutter
{
private:
int inputPinUp;
//...
int _outUp;
int _outDown;
ezButton Button_;
ezOutput Output_;
public:
shutter(ezOutput& outputPin) : Output_(outputPin) {} //ERROR: no default constructor exists for class "ezButton"
shutter(ezButton& Pin) : Button_(Pin) {} //ERROR: no default constructor exists for class "ezOutput"
void shutterInput(
int inputPinUp,...);
void shutterOutput(
int outputPinUp,
int outputPinDown);
void stop(void);
void driveUp(void);
void driveDown(void);
};
I didn't switch the error messages above. They are actually reversed somehow.
shutter.cpp
#include "shutter.h"
//...
void shutter::shutterOutput(
int outputPinUp,
int outputPinDown)
{
int _outUp = outputPinUp; //I am not sure if I even have to do this
}
void shutter::stop(void)
{
ezOutput _outUp.low(); //ERROR: no default constructor exists for class "ezOutput"
digitalWrite(_outDown, LOW);
_moving = false;
}
So I am trying to forward declare a class in my C++ project and then create it in main.
So I have player_obj.cpp which contains the class, classes.h which forward declares the class, and main.cpp which uses it.
classes.h
#ifndef CLASSES_H
#define CLASSES_H
class player_class
{
public:
int x;
int y;
char sprite;
int xprevious;
int yprevious;
private:
bool active;
public:
void update_xy();
player_class(int _x, int _y, char _sprite);
void step();
void destroy();
};
#endif
main.cpp
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
player_class player_obj (5,5,'#');
cout << player_obj.x << ", " << player_obj.y << endl;
return 0;
}
and player_obj.cpp
#include <iostream>
#include <Windows.h>
using namespace std;
class player_class
{
public:
//Coordinates
int x;
int y;
//Sprite
char sprite;
//Previous coordinates
int xprevious;
int yprevious;
//Not everyone can set the activity
private:
//Active
bool active;
//Update xprevious and yprevious - Called by the step event
void update_xy()
{
xprevious = x;
yprevious = y;
}
//All functions public
public:
//Create event/Constructer
player_class(int _x, int _y, char _sprite)
{
//Set default variables
x = _x;
y = _y;
sprite = _sprite;
xprevious = x;
yprevious = y;
active = true;
}
//Step event
void step()
{
//Update old xprevious and yprevious
update_xy();
//Do other stuff here
}
//Drestroy event
void destroy()
{
active = false;
}
};
I thought that would work out all right but when I compile and run it I get:
main.cpp:(.text+0x2c): undefined reference to`player_class::player_class(int, int, char)'
I've done some research, but I can't seem to fix this issue.
I greatly appreciate any help!
Well you're sort of close, what you have in your header is indeed a class declaration (not a forward declaration mind you).
The problem is you never defined it. What you have in player_obj.cpp is an abomination of class redefinition, but you already have your class declared. Just include the header file and define the functions one by one and you're done!
#include "classes.h"
player_class::player_class(int _x, int _y, char _sprite)
{
//Set default variables
x = _x;
y = _y;
sprite = _sprite;
xprevious = x;
yprevious = y;
active = true;
}
// and so on
If you're serious about learning modern C++ though, a few notes:
#pragma once is the modern way of guarding header files. Don't use those #ifdef..#endif constructs.
generally speaking, don't name anything starting with underscores. Especially not parameters visible as part of your public contract.
you have class initializers for a reason, use them! You don't need half a screen of copy pasting variables in your constructors.
You dont want a forward declaration. You want a declaration. It is a classical case of declaring a class in a header file and defining its functions in a cpp file. Then including the header where-ever you want to use your class
You only need forward declarations when you want to use a pointer to that class as a parameter to a function or a member variable somewhere but the definition of that class is not available yet.
Note that when you forward declare a class, you cannot use this class's member variables or functions in that header
-regards
Gautam
The above error is occurring on the line where I implement my constructor. I made a comment that points to that line. Any help greatly appreciated.
#include <vector>
#include <time.h>
class Stopwatch
{
enum state {UNSTARTED, RUNNING, PAUSED, FINISHED};
struct time
{
unsigned hours;
unsigned minutes;
unsigned seconds;
};
struct lap
{
unsigned n; // lap number
time t; // lap time
lap* next_ptr;
};
public:
Stopwatch();
~Stopwatch();
void right_button(); // corresponds to start/stop/pause
void left_button(); // corresponds to lap/reset
private:
state cur_state;
std::vector<lap> lapsvec;
}
Stopwatch::Stopwatch() // <--------- Here's where the compiler error is
{
cur_state = UNSTARTED;
}
/* trivial destructor */
Stopwatch::~Stopwatch()
{
}
int main()
{
return 0;
}
I reviewed C++ constructors to see if I could figure out the problem. No luck.
You need a ; after the class declaration. Since you don't have it, the class is not yet declared when the compiler reaches Stopwatch::Stopwatch(), thus complaining it's a new type.
You are declaring a class, which doesn't have a ; on the end:
...
std::vector<lap> lapsvec;
};
That should fix it:
Well, hi there.
I'm new to c++ and I'm having some issues that I'm not sure what is causing them.
This is my main:
#include "GameWindow.h"
int main(void)
{
GameWindow * game_window = new GameWindow(true);
/* loop the game */
while (game_window->GetRunning())
{
// update
game_window->Update();
// draw
game_window->Draw();
}
delete game_window;
return 0;
}
and this is my header:
class GameWindow
{
private:
bool _running;
//GLFWwindow* _window;
public:
void SetRunning(bool new_val);
bool GetRunning();
GameWindow(bool running);
void Draw();
void Update();
}
and my c++ file:
#include "GameWindow.h"
void GameWindow::SetRunning(bool new_val)
{
_running = new_val;
}
bool GameWindow::GetRunning()
{
return _running;
}
GameWindow::GameWindow(bool running) :
_running(running)
{
}
void GameWindow::Draw()
{
}
void GameWindow::Update()
{
}
While going through all of this I find it tough to find why Visual Studio refuse to compile this code.
It's raising errors about how 'SetRunning' is overloading a function which differs only in return values, and that the return type of main should be Int and not GameWindow, and with all of this I just went completely lost.
Tried to put 'SetRunning' as a comment to simplify the issue but instead it raised the same on 'GetRunning' instead.
I'm guessing it's a really stupid mistake that is easy to fix, but still, can't find it.
Thank you for your time, and I'll appreciate any kind of help.
Missing ; at the end of class definition.
class GameWindow
{
// .....
}; // Missing semi-colon
Missing ; in class defination
{
};
because of this when you include the file in program then compiler not found the end of the class hence it says return type of main should be int not GameWindow
I'm new to C++ and trying to code a HashTable data structure.
I've written it to be generic using templates, and I've included a HashEntry object to use in it to allow for easy quadratic probing for collisions.
The code I have is:
(in a .C file that #include's the below class definition .H file):
HashEntry::HashEntry()
{
this->isActive = false;
}
And the associated .H file with the class definitions is:
#include <iostream>
#include <string>
#include "Entry.C"
using namespace std;
#define Default_Size 50000
class HashEntry;
template <class T> class HashTable
{
private:
int size;
int occupied;
T array[Default_Size];
public:
HashTable();
int Size();
void Add(T t);
void DebugAdd(T t, int index);
T* Get(string index);
/* How do I declare the existence of HashEntry BEFORE here? */
int FindNextOpen(HashEntry he); // Only works for hash_entry objects!
int Hash(string str);
void Rehash();
};
class HashEntry
{
private:
Entry e;
bool isActive;
public:
HashEntry();
HashEntry(Entry e);
bool IsActive();
Entry GetEntry();
};
Whenever I try and compile everything, I get the error for the HashEntry constructor above:
"no matching function for call to Entry::Entry()" ... "candidates are.....".
I have no idea what it means -- when I try to include a default Entry() constructor (my first interpretation), it throws more errors.
Thanks for the help!
UPDATE -- ENTRY.C:
#include "Entry.H"
/* ***Entry Methods*** */
/*
* Overloaded Entry obejct constructor that provides a string value.
*/
Entry::Entry(string s)
{
this->value = s;
this->count = 0;
}
/*
* Returns the number of times this Entry has been accessed/
* found.
*/
int Entry::Count()
{ return this->count; }
/*
* Returns the string value stored in the Entry object.
*/
string Entry::Value()
{ return this->value; }
And the associated .H file with the class definitions is:
#include <iostream>
#include <string>
#include "Entry.C"
Whoa! Never, ever #include a source file in a header.
Your Entry.C should not exist. Instead define the constructor in your header, inside the class definition:
class HashEntry
{
private:
Entry e;
bool isActive;
public:
HashEntry() : isActive(true) {}
...
}
One thing that you haven't shown us is the definition of the class Entry. That is one of the sources of your problem. It's a bit hard to pin down your problem when you didn't show us the very thing that is causing it.
I found the problem.
The error message says there is not matching function call for "Entry::Entry()". Because in no case was I actually creating Entry objects I had no idea what it meant.
I tried adding an explicit default constructor for class Entry and it resolved.
Thanks for the help everyone!