Linker error

When i try to compile i get this error : [Linker error] undefined reference to `Functii::sortare(std::vector<persoana, std::allocator<persoana> >)'
I have my function declared in Functii.h as follows :
#include <vector>
using namespace std;

struct persoana
{ int id;
string nume;
};
class Functii
{
public:
static void sortare(vector<persoana> vp);
.......
And the function body in the Functii.cpp file as follows :
#include "Functii.h"
....
void sortare(vector<persoana> vp)
{
sort(vp.begin(),vp.end(),compare);
for_each(vp.begin(),vp.end(),ffe);
}
And call it from the main.cpp file :
Functii::sortare(vp); //with vp being a vector<persoana>.
I get the same error for another function also and i checked for spelling and case sensitivity when i included the .h files. Also i am using dev c++. Any sugestions pls?
1
2
3
4
5
void sortare(vector<persoana> vp)
{
  sort(vp.begin(),vp.end(),compare);
  for_each(vp.begin(),vp.end(),ffe);
}


This provides a body for a global function named sortare.

You need to provide a body for Functii::sortare. Do this instead:

1
2
3
4
5
void Functii::sortare(vector<persoana> vp)  // <- note the Functii::
{
  sort(vp.begin(),vp.end(),compare);
  for_each(vp.begin(),vp.end(),ffe);
}
In Functii.cpp, you should have:

void Functii::sortare(vector<persoana> vp)
You're right. that was a slip on my account. But the other one was declared in the right scope :

Meniuri.h:
1
2
3
4
5
6
7
8
9
#include <vector>

using namespace std;

class Meniuri
{
      public:
             template <typename T1> static void meniu_sel(vector<T1> vp);
};


Meniuri.cpp:
1
2
3
4
template <typename T1> void Meniuri::meniu_sel(vector<T1> vp)
{
//code
}

with the #include "Meniuri.h"

and called from main.cpp
Meniuri::meniu_sel(vp);

And it gives me the same linker error.
templates can't go in cpp files*.

You need to move that body to a header.


* technically not true, but it might as well be true for what you're trying to do
Ah, i see. It seems to be working once i moved the body. I didn't know that :D Well tyvm for the help and fast reply.
Topic archived. No new replies allowed.