This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited
Related
I am trying to create a class which calls one of it's functions when created, but I am getting the following error when compiling:
g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors -DNDEBUG -c src/PuzzleSolution.cpp
src/PuzzleSolution.cpp:7:32: error: definition of implicitly-declared 'PuzzleSolution::PuzzleSolution()'
PuzzleSolution::PuzzleSolution()
^
src/PuzzleSolution.cpp:12:6: error: prototype for 'void PuzzleSolution::addRow()' does not match any in class 'PuzzleSolution'
void PuzzleSolution::addRow()
^
src/PuzzleSolution.h:19:10: error: candidate is: void PuzzleSolution::addRow(std::vector<unsigned int>&)
explicit PuzzleSolution();
^
src/PuzzleSolution.cpp:17:48: error: no 'void PuzzleSolution::addElement(unsigned int)' member function declared in class 'PuzzleSolution'
void PuzzleSolution::addElement(unsigned int id)
^
make: *** [PuzzleSolution.o] Error 1
Here is the header:
#include <vector>
using namespace std;
class PuzzleSolution {
private:
vector<vector<unsigned int>> sol;
public:
explicit PuzzleSolution();
void addRow();
};
Here is the cpp file:
#include "PuzzleSolution.h"
PuzzleSolution::PuzzleSolution()
{
addRow();
}
void PuzzleSolution::addRow()
{
this->sol.emplace_back();
}
What am I doing wrong?
The code as it is has no error. It compiles with GCC 4.8.2
Be sure that your header file is indeed what you have linked to. Most likely the header being included is different than the one you have actually posted here.
Side Note: Generally it is considered as a bad practice to put using namespace std; in a header file.
Found the issue:
There was a file in the src folder called PuzzleSolution.h.gch
#Quatin and #StoryTeller helped me to understand that this is a pre-compiled header, which the compiler kept using.
Once deleted, the project compiled and executed
I'm writing software to control a bladeRF radio card but I'm running into a strange compiler/linker error that I haven't been able to figure out. My code uses several functions and data structures defined in the library, libbladeRF, but for some reason I can't reference to one specific function.
However, if I modify the call with an improper argument type, g++ will throw an error to let me know that it doesn't conform to the definition, which seems to tell me that the linker is actually able to locate the reference.
What am I missing?
Initial error:
$ g++ bladeRF_test.cpp -o bladeRF_test -lbladeRF
/tmp/ccTWZzdJ.o: In function `enable_xb300()':
bladeRF_test.cpp:(.text+0x36a): undefined reference to `bladerf_xb300_set_amplifier_enable'
Code excerpt:
#include <iostream>
#include <string>
#include <libbladeRF.h>
using namespace std;
...
int set_xb300_pa(bool enable) {
bladerf_xb300_amplifier amp = BLADERF_XB300_AMP_PA;
if ( bladerf_xb300_set_amplifier_enable(dev, amp, enable) ) {
// Print error message
return -1;
} else {
// Print success message
return 0;
}
}
...
Function arguments changed from (dev, amp, enable) to (&dev, amp, enable):
$ g++ blade_hello.cpp -o blade_hello -lbladeRF
blade_hello.cpp: In function ‘int set_xb300_pa()’:
blade_hello.cpp:62:59: error: cannot convert ‘bladerf**’ to ‘bladerf*’ for argument ‘1’ to ‘int bladerf_xb300_set_amplifier_enable(bladerf*, bladerf_xb300_amplifier, bool)
^
In file included from blade_hello.cpp:4:0:
/usr/local/include/libbladeRF.h:2226:15: note: declared here
int CALL_CONV bladerf_xb300_set_amplifier_enable(struct bladerf *dev,
^
I am a bit new to programming as you can probably tell from my prior question(s). I was wondering if anyone could help me with this recent problem I've had. I am trying to compile a script main.cpp using g++ but I get the following errors:
Donny#Donny-PC /cygdrive/c/Users/Donny/Desktop/equation/equations/equations
$ g++ main.cpp -o don.exe
main.cpp:3:11: error: ‘::main’ must return ‘int’
void main(){
^
main.cpp: In function ‘int main()’:
main.cpp:36:22: error: ‘pow’ was not declared in this scope
float n=pow(10.0,9.0);
^
main.cpp:43:27: error: ‘sin’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:43:44: error: ‘tan’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:48:40: error: ‘cos’ was not declared in this scope
d*((pow(tan(theta1),2))/cos(theta1)) +
^
The weird thing is that this code works when compiled with microsoft visual studio 2010 C++. Any help would be greatly appreciated!
EDIT:
So, the fixed a lot of the errors shown above, but I am still having a little difficulty fixing the void main error. Here is how my code looks:
#include<iostream>
#include<cmath>
using namespace std;
void main(){
float r, i, f, beta, alpha;
cout<<"Enter value of R : ";............
Any help or examples would be greatly appreciated.
The first error should be self-explanatory. The standard says that the main function must return int but you have declared it as void. Return 0 from your main function to indicate normal termination. The Microsoft compiler is not as strict on this point.
All your remaining errors can be remedied by using #include <math.h>.
I am trying to write a simple program that uses freetype.
code :
#include <ft2build.h>
#include FT_FREETYPE_H
int main()
{
FT_Library library;
int error = FI_Init_FreeType(&library);
}
i am compiling as g++ try1_c++demo.cpp -I/usr/include/freetype2
it gives following error:
try1_c++demo.cpp: In function ‘int main()’:
try1_c++demo.cpp:9: error: ‘FI_Init_FreeType’ was not declared in this scope
how do i solve this error?
I think I am having a problem with forward declarations. I think one is necessary, but I'm not sure.
Basically I have a main.cpp:
//main.cpp
#include <iostream>
#include "CalculateForces.h"
#include "ParticleBox.h"
int main(void)
{
//g++ main.cpp ParticleBox.cpp -lgsl -lgslcblas -lm -std=c++0x
CalculateForces* calculate_forces= new CalculateForces();
ParticleBox* particles_box = new ParticleBox(2000,100,100,100);
delete calculate_forces;
delete particles_box;
return 0;
}
CalculateForces.h looks like this:
//CalculateForces.h
//We update the forces on each particle
class ParticleBox;
class CalculateForces
{
public:
CalculateForces(void);
~CalculateForces(void);
int UpdateForces(ParticleBox* particlebox);
int DiscretizeSpace(float cutoff_distance);
int LJForce(int local_index, int remote_index, ParticleBox* particlebox);
};
And finally the ParticleBox.h File looks like this:
//ParticleBox.h
//This is the definition of the particlebox. We manage all the particles in this
//file
//This should be changed to a template so that we can run float and double calcs properly :D
struct Particle;
class ParticleBox
{
public:
ParticleBox(int Num_Particles, float Box_length_x_, float Box_length_y_, float Box_length_z_);
~ParticleBox(void);
int set_num_particles(int Num_Particles);
int InitialiseUniverse(int temp,float mass);
float Boltzmann(float temperature);
int GenerateRandomUniquePositions(int number, float max, float min, float* rand_dim_positions);
private:
//Array to hold particles. Each particle has its own struct
Particle** particle_list_;
int num_particles_;
float box_length_x_;
float box_length_y_;
float box_length_z_;
float* rand_x_positions_;
float* rand_y_positions_;
float* rand_z_positions_;
float cutoff_distance_;
float sigma_;
float epsilon_;
};
int CalculateForces::DiscretizeSpace(float cutoff_distance, ParticleBox* particlebox)
{
......
return 0;
}
I use a forward declaration in ParticleBox.h of the Particle Struct and I can add a pointer of type Particle* to the class. This works fine.
The forward in CalculateForces.h of Class ParticleBox causes loads of compiler errors (too many to post but they start in an identical way to the below). Omitting it produces only a few errors:
In file included from main.cpp:3:0:
CalculateForces.h:9:20: error: ‘ParticleBox’ has not been declared
CalculateForces.h:11:50: error: ‘ParticleBox’ has not been declared
In file included from CalculateForces.cpp:3:0:
CalculateForces.h:9:20: error: ‘ParticleBox’ has not been declared
CalculateForces.h:11:50: error: ‘ParticleBox’ has not been declared
CalculateForces.cpp:12:35: error: ‘int CalculateForces::UpdateForces’ is not a static member of ‘class CalculateForces’
CalculateForces.cpp:12:35: error: ‘ParticleBox’ was not declared in this scope
CalculateForces.cpp:12:48: error: ‘particlebox’ was not declared in this scope
CalculateForces.cpp:13:1: error: expected ‘,’ or ‘;’ before ‘{’ token
I thought i would need the forward declaration as I try to use that type as an argument? What am i doing wrong?
Thanks and sorry for the long post
Your post is quite confusing because you posted the error that show up when you omit the forward declaration and you obviously have some additional errors in your code that mix with the error you asked about.
I assume that with the forward declaration, the errors change as they appear mostly in the implementation files, right? In this case the problem might be that the forward declaration is enough as long as you declare a pointer to the type, but it is not enough when you start using the pointer (dereferencing it).
If that is the case, the problem is most likely that you forgot to #include "ParticleBox.h" in CalculateForces.cpp (or some other implementation files).
As Rob determined the source of the errors was in a bit of code that I did not post. There were a few errors but the biggest was that in CalculateForces.cpp I tried to access int num_particles_; which is of course a private member of ParticleBox.