I had a function to get points inside a 3D arc working inside a file but, trying to order the code into the appropriate folders, I broke it, and I don't know why because I think I am including it correctly.
Originally a lot of calculations were inside a file "messages.cpp", but it is supposed to have just the messages and the calculations should be in Calculations folder.
So now messages.cpp is something like this:
#include "../../FGProcessorModule/Calculations/ArcToPoints.h"
namespace myNamespace {
void MsgProvider::onEvent{
std::vector<Formulas::LLPoint> allPoints = ArcToPoints(center, start, end);
}
}
In "../../FGProcessorModule/Calculations/ArcToPoints.h" I have:
#ifndef ARCTOPOINTS_H
#define ARCTOPOINTS_H
#include blablabla
namespace myNamespace{
std::vector<Formulas::LLPoint> ArcToPoints (Formulas::LLPoint, Formulas::LLPoint, Formulas::LLPoint);
}
#endif /* ARCTOPOINTS_H */
And finally in "../../FGProcessorModule/Calculations/ArcToPoints.cpp" I have:
#include "ArcToPoints.h"
namespace myNamespace{
std::vector<Formulas::LLPoint> ArcToPoints (Formulas::LLPoint center, Formulas::LLPoint start, Formulas::LLPoint end){
//Lots of calculations
}
}
I think everything is OK but I receive this error when I want to compile:
undefined reference to `FVIS::ArcToPoints(Formulas::LLPoint,
Formulas::LLPoint, Formulas::LLPoint)'
I don't know if this will help others as it is a very specific problem, this is a project using ros and catkin.
I was missing including the new created file in CMakeLists.txt. Now it compiled just fine.
Related
I have two files, main.ccp and namespace.cpp (Not the actual names, but for simplification)
My namespace.cpp can be boiled down to the following:
#pragma once
namespace MyNamespace {
int A = 0;
}
And my main.cpp basically just uses the content of MyNamespace:
#include "namespace.cpp"
int main() {
...
// Using MyNamespace::A
...
}
But I keep getting a LNK2005 error in MyNamespace.obj, that A is already defined in main.obj.
Which is weird: It's not like I've defined it twice. I'm literally just using it in my main file, but since I wanted to clean up my main file a little bit I put it into it's own file and into a namespace.
I don't understand where the problem is, would appreciate help.
Thanks
I have been recently practicing managing multiple objects and drawing them in C++ using SFML library. I wanted my textures and future resources to be more reusable so I decided to make use of Thor library which suits my needs really well.
So I've written first few lines of code based on what you can find in this tutorial and the compiler always says:
main.cpp|12|error: 'textures_holder' does not name a type
This line gives an error :
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
I'm using Code::Blocks IDE with MinGW compiler and SFML 2.5.0.
Here's my main.cpp and the header file which contains extern object :
//...
#include <Thor/Resources.hpp>
#include "Dirt_Block.h"
using namespace std;
//Adding textures to the texture library
//THIS LINE GIVES AN ERROR
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//Rest of code...
Dirt_Block.h (only the upper part) :
#ifndef DIRT_BLOCK_H
#define DIRT_BLOCK_H
#include <SFML\Graphics.hpp>
#include <vector>
#include <Thor/Resources.hpp>
#include <Thor/Resources/SfmlLoaders.hpp>
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
//Rest of the code
I'd like to know what is causing this error and maybe help others who may experience similiar frustrating problems. Thanks for help.
EDIT :
As suggested in the comment I've declared a few extern int variables in the Dirt_Block.h so now it looks like this :
//...
extern int test_int_up;
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
extern int test_int_d;
//...
And then assinged to them some value in main.cpp :
//...
test_int_up = 55;
test_int_d = 55;
//Adding textures to the texture library
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//...
But the compiler gives error :
main.cpp|9|error: 'test_int_up' does not name a type
main.cpp|10|error: 'test_int_d' does not name a type
main.cpp|12|error: 'textures_holder' does not name a type
Much less distracting to see what your problem is without all the extraneous code!
C++ programs don't start from the top of the file and run code down to the bottom. They start at the main(), and control flow proceeds from there, with one thing triggering another.
(Note: That doesn't take into account global constructor ordering, which does go in order of declaration--but you have no guarantee of the order declarations from "different files" might run in.)
Point being, you can't just make random function or method calls in the middle of a file. That's where you put declarations. You have to be inside of a function or method to make calls, e.g.
int main() {
textures_holder.acquire(
"Dirt",
thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png")
);
...
}
This is my code:
#include <eigen3/Eigen/Geometry>
#include <stdio.h>
int main(){
Eigen::Matrix3Xd in(3, 100, 100), out(30, 100, 0);
Eigen::Affine3d A;
A = Find3DAffineTransform(in,out);
return 0;
}
and I am using the functions provided by wiki:
https://github.com/oleg-alexandrov/projects/blob/master/eigen/Kabsch.cpp#L4
Basically, I was trying to get the rotation matrix based on the input and output points given.
And this the error I got:
Well, from your code it does not seem that you are actually using this code from github.
You should either copy-paste the contents of Kabsch.cpp to your application above main (this is a quick'n'dirty solution), or you should:
Add Kabsch.cpp to your build (meaning it should be compiled and linked with your main file
Forward declare Find3DAffineTransform function above your main (or put the declaration in a separate hpp file and include it from your main file
[EDIT:]
The problem seems to belong to the functions, that take default-parameters. Without separating in *.h *.cpp and main file it worked as i implemented something like:
void foo(double db;); // deklaration
void foo(double db = 4){ cout << db;} // definition
int main(){
foo(); // usage
return 1;
}
But if I separate deklaration (-> *.h), definition (-> *.cpp) and usage (-> main) compiling suddenly returns an erro telling, there is no function foo(void), as it does not recognize that there is a default parameter. Any suggestions for that?
[/EDIT]
I wrote a c++-program running somehow like:
#include <iostream>
/* other includes */
using namespace std;
class my_class
{
private:
/* variables */
public:
/* function deklarations (just some short ones are only defined not declared) */
};
ostream& operator<<(ostream &out, my_class member);
/* Definition of the member functions and of the not-member-function */
int main()
{
/*some trial codes of member-functions */
return 1;
}
In one total file all compiled well in Eclipse and worked. Now I also wanted to try seperate in a main,class-header and class-cpp file (called them "my_class.h" and my_class.cpp").
For that i put in class-header:
#ifndef MY_CLASS_H_
#define MY_CLASS_H_
#include <iostream>
/* other includes */
using namespace std;
class my_class
{
/* ... */
};
ostream & operator<<(ostream &out, my_class member);
#endif /* MY_CLASS_H_ */
I put in class-cpp:
/* Definition of the member functions and of the not-member-function */
I put in main:
#include <iostream>
#include "my_class.h"
#include "my_class.cpp"
int main()
{
/*some trial codes of member-functions */
return 1;
}
This version is compiling with the g++ command in commandline:
g++ -o main.exe main.cpp
But it does not Compile in Eclipse. There it gives me the Error:
...\my_class.cpp:11.1: error: 'my_class' does not name a type
and same for all other member functions and variables. I tried to follow the instructions from here (I put just "my_class.h" in main and my_class.cpp, but then it did not compile in Eclipse and in command line (of course then with the my_class.cpp included). Eclipse gives me an Error, that makes me believe Eclipse does not see the "my_class.cpp":
...\main.cpp:288:47: error: no matching function for call to 'my_class::foo(...)'
where foo stands for the first member-function declard in the "my_class.cpp" file. First It gave the error for the constructor too, but as I put it's definition directly into the *.h file it worked well. (That's why I think, it does not see the "my_class.cpp" file)
I think I might be missing something very trivial as I am very new to Eclipse, but I don't see it. I tried to make my questions and information as short as possible.
default-parameters need to be declared in the header-file as it contains the declarations and not in the cpp file, which contains the definitions. (An additional mistake was to declare them in the definition). Found some help here. But why did it work, as I implemented it in one whole file?
Answer:
If default-parameter is in the cpp-file, the main file does not see it as
it looks only into the header-file
But if the whole code is included in just one file, the default-value
can be found in the definition too.
To explain myself:
I considered answering my question, because it gives a better overview of the whole question and the question will now not appear as unanswered. After reading this, I think that it is the right way to do so.
I"m having some problem with the following program. The program implements a stack using a linked list. I'm not showing all my code here because the code is fine. But the problem I'm having is with linking different files together.
I'm using an IDE to run the program. When I run the TestIntStacks.cpp, the main method is supposed to call test() from StackFunctions.cpp. The test function (defined in StackFunctions.cpp), uses the TestStack class methods.
Currently I'm receiving an error saying "linker error, push/pop not defined". What I'm doing wrong? I'm sure it's something to do with a namespace.
MyStack.h
-------------------------------------
namespace A
{
class Node{
public :
char data;
StackNode* link;
StackNode(int v=0): data(v), link(NULL){ }
};
class MyStack{
private:
Node * top;
public:
MyStack():top(NULL){ }
void push(int c);
};
}//namespace
//TestStack.cpp
--------------------------------------------------------------
#include "MyStack.h"
namespace A
{
void MyStack::push(int x)
{
StackNode *temp = new StackNode(x);
temp->link = top;
top = temp;
}
}
//StackFunctions.cpp
-----------------------------------------------------------
#include <iostream>
#include "TestStack.h"
using namespace std;
using namespace A;
void test()
{
MyStack st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
}
// TestIntStacks.cpp
----------------------------------------------------------------
// Code for testing the TestStack
// from the A namespace.
#include <iostream>
#include <vector>
using namespace std;
#include "TestStack"
#include "StackFunctions.cpp"
void test();
int main()
{
test();
system("pause");
return 0;
}
You are defining push() and pop() methods in your header file TestStack.h, but you've not provided implementations for them in TestStack.cpp. You need to add the code that does the push and pop operations on your object.
This error seems pretty clear to me. You declared push() and pop() in your header file, but the linker could not find where these methods are implemented.
Where are they defined?
I think it has to do with the arguments provided to linker. For example, a similar error occurs when you use Visual C++ 6 in a following way. Let's say you created .cpp and .h files for a class. If you do not include cpp file into your project you get the similar error. Because the IDE does not determine the source file based on the provided header file. I don't know about dev-c++ IDE, but the solution might be similar. The problem is you compile (or not) TestStack.cpp and the output of this compiling is not provided to the linker, so the linker can't find the implementation.
You need to force the build script to use both cpp files. If you wrote your own make file, you need to build intermediate objects for each source, and then link at the end.
I suspect DEV-C++ doesnt automatically generate object files or try to link everything together.