Sean
Sean

Reputation: 967

Template and vector errors

I dont know what im doing wrong this is my first time seperating .cpp files and using templates and vectors. I keep getting these two errors: error C2143: syntax error : missing ',' before '<' and error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

main.cpp

#include <iostream>
#include <vector>
using namespace std;

template <typename T>
void write_vector(const vector<T>& V);

int main()
{
    int n;
    int value;
    vector<int> V;
    cout << "Enter n: ";
    cin >> n;
    cout << "Enter " << n << " integer values: ";
    while(V.size() < n && cin >> value){
        V.push_back(value);
    }

    write_vector(V);

    return 0;
}

writeVector.cpp

template <typename T>
void write_vector(const vector<T> &V)
{
    for(int i=0; i < V.size(); i++)
        cout << V[i] << " ";
}

Upvotes: 0

Views: 255

Answers (6)

FailedDev
FailedDev

Reputation: 26930

On the top of you main.cpp add :

#include "Vector.cpp" // since you are learning, this is really strange and should be avoided, but for your example we leave it as is.

In your Vector.cpp

#include <vector>
#include <iostream>

template<class T>
void write_vector(const std::vector<T>& V)
{
    for(int i=0; i < V.size(); i++)
        std::cout << V[i] << " ";
}

Do not use using statements. Bad habit. You don't need explicit instantiation or other stuff. Your compiler can deduct the correct template.

Altough this works, it's bad code. When using template always write everything in the .h file. So change your Vector.cpp to Vector.h. Remove the forward declaration from your main.cpp and #include "Vector.cpp"

Your code will work fine without any other changes.

Finally the C++ way of doing things would be e.g. :

#include <vector>
#include <iostream>
#include <algorithm>

template<class T>
void write_vector(const std::vector<T>& V)
{
    std::copy(V.begin(), V.end(), std::ostream_iterator<T>(std::cout, " ")); // you don't even need this function right?
    //for(int i=0; i < V.size(); i++)
 //       std::cout << V[i] << " ";
}

You can do similar things for the cin.

Upvotes: 0

user213313
user213313

Reputation:

write_vector needs to be in a header file (writeVector.h) rather than a .cpp file if you want to share it between different modules. For more information about why you need to do this see the C++ FAQ.

I presume that your syntax error is because you have a missing using namespace std; in writeVector.cpp? When you move it to a header file you should not put this using directive there because that will needlessly pollute the global namespace whenever this file is included. Instead you should explicitly qualify all references to vector as std::vector.

Upvotes: 0

koan
koan

Reputation: 3686

Your template function must be defined in advance so that the compiler can use it. Either define it before main() in the single file or #include it in a header file.

In summary, template functions should be defined in header files. See the answer to Why can templates only be implemented in the header file?

Upvotes: 3

Christian Horsdal
Christian Horsdal

Reputation: 4932

As far as I remember you cannot separate the declaration and implementation of a template.

Upvotes: 1

smparkes
smparkes

Reputation: 14063

You need the includes and using in both files. At that point, you might need the explicit instantiation mentioned in other answers, but those will show up as linker issues.

Upvotes: 0

Codie CodeMonkey
Codie CodeMonkey

Reputation: 7946

You need explicit instantiation to do this because the compiler doesn't know what types the template needs to be compiled for when compiling the .cpp. See Explicit instantiation - when is it used?.

Upvotes: 3

Related Questions