Skip to content

Instantly share code, notes, and snippets.

@DavidYKay
Created July 7, 2016 21:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DavidYKay/22226bff98cba3d8b1223bd200bb12bc to your computer and use it in GitHub Desktop.
Save DavidYKay/22226bff98cba3d8b1223bd200bb12bc to your computer and use it in GitHub Desktop.
C++ Class / Header file example
#import "Car.h"
const int ACCELERATION_FACTOR = 10;
const int BRAKING_FACTOR = 30;
Car::Car() {
speed = 0;
}
void Car::accelerate(float intensity) {
speed = speed + ACCELERATION_FACTOR * intensity;
}
void Car::brake(float intensity) {
speed = speed - BRAKING_FACTOR * intensity;
}
class Car
{
public:
Car();
void accelerate(float intensity);
void brake(float intensity);
private:
int speed;
}
Car *myCar = Car::Car();
myCar->accelerate(1.0);
myCar->brake(0.5);
@zippy4166
Copy link

please help

I cannot get this to compile

panel.h file is

class panel
{
private:

int x;   
int y;  
int width;   
int height;
     
public:
      
/** construction*/

panel();
void set_x(int x);

};
panel.cpp is

#include"panel.h"

panel::panel()
{
this.x = 0;

}/** end construction */

void panel::set_x( int x )
{
this.x = x;

}/** end method */

tester

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"panel.h"

int main()
{
panel*p;

	p =  new panel();
	p ->set_x(10);
	
return 0;

}

results are
simon@xyrix:~/screensaver$ g++ -o test test.cpp -L/usr/lib -lX11
/tmp/ccv6RdUG.o: In function main': test.cpp:(.text+0x27): undefined reference to panel::panel()'
test.cpp:(.text+0x3a): undefined reference to `panel::set_x(int)'
collect2: error: ld returned 1 exit status

please help
Si

@uroshm
Copy link

uroshm commented Aug 21, 2018

Can you re-post with cleaner look ? Difficult to understand the issue from this copy/paste.

@JSquar
Copy link

JSquar commented Oct 11, 2018

Try

#include"panel.h"
panel::panel()
{
    this->x = 0;
}/** end construction */

void panel::set_x( int x )
{
    this->x = x;
}/** end method */

and build with g++ -o main.x main.cpp panel.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment