I attempted to compile this Arduino sketch with a custom library in it (jtaServoController), and the Arduino IDE claims that my constructor in the Arduino sketch was written incorrectly (more precisely, 'jtaServoControllerFour' does not name a type).
#include <jtaServoController.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
#define SERVOMIN 0
#define SERVOMAX 600
jtaServoControllerFour pointerFinger(1,2,3,4); //line in question
void setup() {
Serial.begin(9600);
pwm1.begin();
pwm1.setPWMFreq(60);
}
void loop() {
pointerFinger.jtaSetPWMFour(300,400,500,600);
}
My Question is whether the line in question actually is written wrong or is there an issue in another part of my code?-probably in the library itself which I have below. (btw I found the information for constructing an object in the Arduino tutorial on libraries).
These are the Header and .cpp files respectively:
#ifndef jtaServoController_h
#define jtaServoController_h
#include "Wire.h"
#include "Adafruit_PWMServoDriver.h"
#include "Arduino.h"
class jtaServoControllerFour
{
public:
jtaServoControllerFour(int servo1, int servo2, int servo3, int servo4);
void jtaSetPWMFour(unsigned int servo41, unsigned int servo42, unsigned int servo43, unsigned int servo44);
private:
int _servoOne;
int _servoTwo;
int _servoThree;
int _servoFour;
};
#endif
CPP file
#include "Arduino.h"
#include "jtaServoController.h"
#include "Adafruit_PWMServoDriver.h"
#include "Wire.h"
jtaServoControllerFour::jtaServoControllerFour(int servoOne, int servoTwo, int servoThree, int servoFour)
{
_servoOne = servoOne;
_servoTwo = servoTwo;
_servoThree = servoThree;
_servoFour = servoFour;
}
void jtaServoControllerFour::jtaSetPWMFour(int servoFourOne, int servoFourTwo, int servoFourThree, int servoFourFour)
{
pwm1.setPWM(_servo1, 0, servoFourOne);
pwm1.setPWM(_servo2, 0, servoFourTwo);
pwm1.setPWM(_servo3, 0, servoFourThree);
pwm1.setPWM(_servo4, 0, servoFourFour);
return;
}
#include <jtaServoController.h>
You may need to use the following instead (double quotes):
#include "jtaServoController.h"
And/or check the path to your header file.
See here for the difference between the two.
Related
So I'm implementing a simple test class and such I'm separating files into :
compte.h:
#ifndef COMPTE_H
#define COMPTE_H
#include <iostream>
class compte
{
public:
static int n;
int numCompte;
char* nom;
double solde;
public:
compte(const char* = NULL, const double & = 0);
~compte();
};
and a compte.cpp
#include <iostream>
#include <cstring>
#include "compte.h"
using namespace std;
int compte::n = 1000;
compte::compte(const char* nom, const double &solde)
{
this->solde = solde;
this->nom = new char[strlen(nom)];
strcpy(this->nom, nom);
numCompte = n++;
}
compte::~compte()
{
delete[] nom;
}
however, when I include compte.h I get an unidentified reference to the member methods of the class, when I include compte.cpp it works, I just want to know what I can add to include the .h file instead of .cpp
Your header file compte.h is missing an #endif in the end.
#ifndef COMPTE_H
#define COMPTE_H
#include <iostream>
class compte
{
public:
static int n;
int numCompte;
char* nom;
double solde;
public:
compte(const char* = NULL, const double & = 0);
~compte();
};
#endif
This is a sample of main file you can test with
#include "compte.h"
#include <memory>
using namespace std;
int main()
{
unique_ptr<compte> account = make_unique<compte>("account", 100);
cout << account->solde;
}
Compile and run:
g++ compte.h compte.cpp main.cpp -o main.exe
./main.exe
as a rule of thumb do NOT include *.c or *.cpp.
You should include only headers of f. (and classes for C++), since the extension: "headers"
including *.ccp may work, if you include it ONCE in only one other file (in you example near main()), but it can lead to duplication of identifiers, as including in 2 files, will produce TWO source code (work dine by preprocessor) so TWO functions (classes...) code, so link will complain about it.
Relearning C/C++ after 3 years of JavaScript (I've gotten way too comfortable..)
I'm building a test file with input.
The problem is within cTool, where the first function is not letting me return a string. I thought this was totally valid if the library is included in the header file? What am I overlooking here.
cTool.cpp
string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
You need to add #include "cTool.h" to cTool.cpp, not just to cMain.cpp only. Otherwise, when compiling cTool.cpp, the compiler doesn't know what a string is since it doesn't see your #include <string> and using namespace std; statements (BTW, using namespace std; in a header file is a very bad idea).
cTool.cpp
#include "cTool.h" // <-- ADD THIS!
std::string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <iostream>
std::string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
I've got three classes in a project I'm working on called Pixel, custFrame, and FrameHolder.
My custFrame class header is like so:
#pragma once
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "Pixel.h"
#ifndef CUSTFRAME_H
#define CUSTFRAME_H
class custFrame
{
public:
custFrame();
void addPixel(Pixel pix);
void setWidth(int width);
void setHeight(int height);
int getWidth();
int getHeight();
int getPixelSize();
Pixel getPixel(int count);
private:
std::vector<Pixel> pixels;
int Height;
int Width;
};
#endif
and my FrameHolder class header is like so:
#pragma once
//Hold all captured frames containing data
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "custFrame.h"
#include "Pixel.h"
#ifndef FRAMEHOLDER_H
#define FRAMEHOLDER_H
class FrameHolder {
public:
FrameHolder();
static FrameHolder* instance();
void addFrame(IDeckLinkVideoFrame* fram);
void calibrate(custFrame fram);
int numFrames();
void setWidth(int width);
void setHeight(int height);
static FrameHolder *inst;
bool calibrating;
int getHeight();
int getWidth();
bool isCalibrating();
private:
//Member variables
int Width;
int Height;
std::vector<IDeckLinkVideoFrame *>frames;
};
#endif
In my FrameHolder class passing a custFrame object to a function to store that frame in the object does not seem to work. I get a compiler error ("syntax error: identifier 'custFrame' line 24"). However in my custFrame class, passing a Pixel object to be stored as part of a frame works wonderfully. Am I missing something? I've seen this post but it didn't help much.
The problem is caused by the presence of
#include "FrameHolder.h"
in both the .h files. Because of that, the definition of custFrame is not seen before the definition of FrameHolder.
Passing by pointers/references is probably what you should be doing here. As for the syntax error, it might be that the custFrame class is not declared properly when you include it in the header.
I am farily new to C++ and I have been stuck with this problem for a few hours now. I am trying to setup the foundations for a video game related experience calculator, but I can't get past this problem.
main.cpp
#include <iostream>
#include "Log.h"
using namespace std;
int main()
{
Log Logs;
enter code here
struct ChoppableLog Yew;
Logs.initialiseLog(Yew, 60, 175);
return 0;
}
Log.h
#ifndef LOG_H
#define LOG_H
struct ChoppableLog
{
int level;
int xp;
};
class Log
{
public:
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int);
Log();
};
#endif // LOG_H
Log.cpp
#include "Log.h"
#include <iostream>
using namespace std;
Log::Log()
{
}
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}
The error I get is
C:\Users\Murmanox\Documents\C++\C++ Projects\CodeBlocks\Class Files Test\main.cpp|11|undefined reference to `Log::initialiseLog(ChoppableLog&, int, int)'|
I can post more details if necessary.
You have to define Log::initialiseLog with its full name, like so:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{ }
What you are doing is defining a new, free function of the name initialiseLog instead of defining the member function of Log.
This leaves the member function undefined, and, when calling it, your compiler (well, technically linker) will be unable to find it.
The definitions of functions in a header file should specify the scope. In your case, you should define initialiseLog() function in your cpp file as follows:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}
I have a class called pos... I am trying to poll a method from this class. I used pthread_create(pthread_t thread, pos::Pirnt_data,this);
I get an error that pos is not declared in the scope... I included the h file of pos but I don't understand. I think I am using a wrong format can somebody help me
#include "position.h"
#include "pthread.h"
#include "pos.h"
void position::tick(schedflags_t flags)
{
if(pthread_create(&thread,NULL,pos::Print_data,this)!=0) {
stringstream bad;
bad << "OPIMex: Could not create listener thread: "
}
this class position has method tick that runs every 1 second with the data. I am trying to poll a method Print data from the class pos but it gave me that error any ideas why ?
this is class pos.h
#ifndef POS_H_
#define POS_H_
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <gps.h>
#include <string.h>
#include <pthread.h>
#include <string>
#include <vector>
#include <strings.h>
#include <math.h>
using namespace std;
namespace herpderp {
namespace modules {
int UBX_step =0;
long data;
int UBX_class;
int UBX_id=0;
int UBX_payload_length_hi;
int UBX_payload_length_lo;
int UBX_payload_counter =0;
int ck_a;
int ck_b;
int GPS_timer;
int fd;
unsigned int UBX_buffer[35];
int payload_data;
long lat=0;
long lon=0;
long alt_MSL=0;
long iTOW=0;
long alt=0;
unsigned long LastMS;
int UBX_Read;
vector <float> v;
fstream myfile;
int Open_port(void);
int read_tofile();
long join_4_bytes( unsigned int Buffer[]);
void parse_ubx_gps(void);
void checksum(char ubx_data);
void Print_data();
int push_data_into_vector();
int decode_gps();
int Configure_gps();
int test();
int Close_NEMA();
int Open_UBX();
}
}
#endif //POS_H_
pthread_kill is not on pthread.. It is on signal.h
#include <signal.h>
1) you can provide some code snippet/additional information to help you better.
2) If you are getting linkage error, check if you have linked with -lpthread library.
Form the pos.h it seems that there is no class called pos and you just need to call the function name:
if(pthread_create(&thread,NULL,Print_data,this)!=0) {