Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 26 days ago.
Improve this question
I'm a bit lost here and don't really get why i run into this compile error.
I have a Arduino Nano Project set up with platform.io
My main.cpp looks like this:
#include <WiFiNINA.h>
#include "MainController.h"
void setup()
{
MainController mainController;
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
mainController.begin();
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, LOW);
delay(1000);
}
void loop()
{
//mainController.update();
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
}
MainController.cpp:
#include <MotorController.h>
class MainController {
public:
MainController() {
}
void begin() {
motorController.begin();
}
void update() {
}
private:
MotorController motorController;
};
MainController.h:
#ifndef MAINCONTROLLER_H
#define MAINCONTROLLER_H
#include "MotorController.h"
// #include "ServoController.h"
class MainController
{
public:
MainController();
void begin();
void update();
private:
MotorController motorController;
// ServoController servoController;
};
#endif
So, when i try to compile it i get this compile error:
/Users/mneuhaus/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: .pio/build/nanorp2040connect/src/main.cpp.o: in function `setup':
main.cpp:(.text.setup+0x4): undefined reference to `MainController::MainController()'
/Users/mneuhaus/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: main.cpp:(.text.setup+0x28): undefined reference to `MainController::begin()'
collect2: error: ld returned 1 exit status
*** [.pio/build/nanorp2040connect/firmware.elf] Error 1
as far as i can tell all the classes, methods, etc are named correctly, no idea why i get these errors.
You don't need to repeat the class definition inside the cpp file. The implementation should loook like this:
#include "MainController.h"
void MainController::begin() {
motorController.begin();
}
// ...
I would also recommend removing the constructor if it does not do anythng.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I created a class to manage commands thru serial port and i am using a stepper motor but when i am trying to use step function is not working.
I declare an object in arduino file and i used pointer of stepper object to wotk inside class. testing it, setSpeed method works fine but when i try to use step method, i got segmentation fault on compiling action.
Stepper motor library is working ok, i already did some test and motor works fine but when i tried to use it in a class with pointers is not working.
main file
#include "ClassTest.h"
ClassTest test;
Stepper myStepper1 = Stepper(200, 8, 9, 10, 11);
void setup() {
test.SetupMotor(&myStepper1);
}
void loop() {
test.MoveMotor('Motor1',200);
}
ClassTest.h
#include "Arduino.h"
#include "Stepper.h"
class ClassTest
{
public:
ClassTest();
void SetupMotor(Stepper* step);
void MoveMotor(String ,int );
private:
Stepper* _myStepper1;
};
ClassTest.cpp
void ClassTest::SetupMotor(Stepper* step)
{
_myStepper1=step;
_myStepper1->setSpeed(200);
}
void ClassTest::MoveMotor(String motor,int stepCount)
{
// i am getting an issue on compiling time about segmentation fault
_myStepper1->step(200);
}
I tried many things but not sure why is not working yet, any help???
Thanks!!
I'm unable to reproduce your error. The following code compiles in Arduino IDE:
#include "Arduino.h"
#include "Stepper.h"
class ClassTest
{
public:
ClassTest() = default;
void SetupMotor(Stepper* step);
void MoveMotor(String, int );
private:
Stepper* _myStepper1;
};
void ClassTest::SetupMotor(Stepper* step)
{
_myStepper1 = step;
_myStepper1->setSpeed(200);
}
void ClassTest::MoveMotor(String motor, int stepCount)
{
_myStepper1->step(200);
}
ClassTest test;
Stepper myStepper1 = Stepper(200, 8, 9, 10, 11);
void setup() {
test.SetupMotor(&myStepper1);
}
void loop() {
test.MoveMotor("Motor1", 200);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I have test.h which contains:
#ifndef TEST_H_
#define TEST_H_
class test {
public:
int value;
};
#endif /* TEST_H_ */
and my main.cpp:
#include "test.h"
class Magic {
test x;
x.value = 2; // Syntax error
};
int main () {
test y;
y.value = 2; // Works fine
return 0;
}
Why is this happening?
Assigning values like that is not valid syntax in a class definition in c++. The error has nothing to do with headers or whatever. Try putting everything in a single file and you will see the same behavior.
If you want to have default initialization of x.value to 2 for each instance of Magic define this in the constructor of Magic:
class Magic {
test x;
Magic() {
x.value = 2;
}
};
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm getting the following errors:
C:\Users\James\cavewhere\dewalls\src\unit.cpp:6: error: C2511: 'dewalls::Unit::Unit(QString,dewalls::UnitType *)' : overloaded member function not found in 'dewalls::Unit'
C:\Users\James\cavewhere\dewalls\src\unit.cpp:9: error: C2550: 'dewalls::Unit::{ctor}' : constructor initializer lists are only allowed on constructor definitions
unit.h:
#ifndef UNIT_H
#define UNIT_H
class UnitType;
#include <QString>
namespace dewalls {
class Unit
{
public:
Unit(QString name, UnitType *type);
private:
QString _name;
UnitType *_type;
};
}
#endif // UNIT_H
unit.cpp:
#include "unit.h"
#include "unittype.h"
namespace dewalls {
Unit::Unit(QString name, UnitType *type) :
_name(name),
_type(type)
{
}
}
What for the love of god am I doing wrong?
Looks like UnitType should be in your namespace too
namespace dewalls {
class UnitType;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have written a class:
CVerifObj.hpp:
#pragma once
#include <vector>
class VerifObj
{
private:
cv::Mat m_image;
PointVector m_plateContour;
std::string m_imageName;
public:
VerifObj(const fs::path& imgNameIn);
~VerifObj();
cv::Mat getImage() const;
std::string getImageName() const;
PointVector getPlateContour() const;
};
typedef std::vector< VerifObj > VerifObjVector;
That has implementation and that is used as a type of another function in another class that includes its header:
MyCls.hpp:
#pragma once
#include "CVerifObj.hpp"
class MyCls
{
public:
MyCls();
~MyCls();
static VerifObjVector foo(); // error is here
};
The problem I get is that it is not recognized:
/home/sop/proj/CMyCls.hpp:52:2: error: ‘VerifObjVector’ does not name a type
I have added it in the CMake file too. Why is this happening?
You haven't included the std::vector definition:
#include <vector>
You're probably including MyCls.hpp in CVerifObj.hpp, directly or indirectly, which leads to a circular include. This can cause issues (undefined types).
Remove circular includes by using forward declarations.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include<iostream>
#include<string>
using namespace std;
#ifndef TicTac_H
#define TicTac_H
class TicTac
{
public:
TicTac(int ,int);
void setpos(int);
void getpos(int);
void setpos2(int);
void getpos2(int);
bool takepos();
void setar(int&, int&);
void setarr();
void all(int,int);
void print();
int test();
private:
int p1;
int p2;
string tic[3][3] ;
string x;
string o;
int t1;
int t2;
bool ok;
};
#endif
**The compiler shown this message :
no default constructor exists for class "TicTac"
'TicTac' : no appropriate default constructor available
can anybody help me to fix this problem **
The error is surely not in that code, but in the code that includes that header and attempts to create an object of type TicTac without providing the two arguments that the constructor takes (two int). Other than that, the include guards should cover all the file (including the #include<...>) and you should never have a using directive (using namespace X) in a header.