0

I have the following in a header file. A template class with many functions.

#pragma once
#include <string>
using namespace std;    

template <class T>
class Vector3Generic
{
public:
    T x;
    T y;
    T z;

    Vector3Generic();
    ~Vector3Generic();
    Vector3Generic(T vx, T vy, T vz);
    Vector3Generic(Vector3Generic& v);
    // 
    // ... many functions, some static, and some are not.
    static T Vector3Generic::Distance(const Vector3Generic& a, const Vector3Generic& b);
    static T Vector3Generic::Slope(const Vector3Generic& linepoint1, const Vector3Generic& linepoint2);

};      
template <class T>
T Vector3Generic<T>::Distance(const Vector3Generic<T>& a, const Vector3Generic<T>& b)
{
    auto vector = Vector3Generic<T>(a.x - b.x, a.y - b.y, a.z - b.z);
    return sqrt(((vector.x * vector.x) + (vector.y * vector.y)) + (vector.z * vector.z));
}
template <class T>
T Vector3Generic<T>::Slope(const Vector3Generic<T>& linepoint1, const Vector3Generic<T>& linepoint2)
{
    return (linepoint2.z - linepoint1.z) / (linepoint2.x - linepoint1.x);
}

My problem is that I only get the error message for the included functions above. I cannot figure out the problem.

Can you see what is wrong?

Aaron Azhari
  • 987
  • 1
  • 10
  • 24
  • 4
    do not qualify member functions' names with `Vector3Generic::` within class scope – Piotr Skotnicki Oct 18 '14 at 21:15
  • why even split them, just implement the templates inside the class – Creris Oct 18 '14 at 21:18
  • 1
    @CaptainObvlious no code is using this class yet. Why is that relevant? – Aaron Azhari Oct 18 '14 at 21:22
  • @PiotrS. It worked! But why is that? I have around 20 functions and only these two complained. For all other I still use Vector3Generic:: for the names and it compiles fine. – Aaron Azhari Oct 18 '14 at 21:26
  • @AramAzhari what compiler? – Piotr Skotnicki Oct 18 '14 at 21:30
  • @PiotrS.MsVC++ (Visual Studio 2013) – Aaron Azhari Oct 18 '14 at 21:34
  • @AramAzhari I can't tell without code, are those other "working" methods static as well? are you actually using them in the code? – Piotr Skotnicki Oct 18 '14 at 21:36
  • These are some that work: `template Vector3Generic Vector3Generic::Normalized() const { T num = Vector3Generic::Magnitude(); if (num > 1E-05f) { return (*this / num); } return Vector3Generic::Zero(); }` I haven't started using them. I had a float class implementation and I wanted to make it work with double and int. If I actually write the implementation in a separate cpp file, then I have to put the Vector3:: on the function names or it won't compile (for a non-template implementation) – Aaron Azhari Oct 18 '14 at 21:42
  • @PiotrS.In fact if I remove the Vector3Generic:: from other function names I get "modifiers not allowed on nonmember functions" – Aaron Azhari Oct 18 '14 at 21:53

0 Answers0