0

I keep having this issue with this program involving templates. I apologize for posting the entire code, but it's extremely short. The problem I keep having is it claims there's an error in c2872.

driver.cpp

#include <iostream>
#include "pair.h"
using namespace std;


int main()
{
    pair<char> letters('a', 'd');
    cout << "\nThe first letter is: " << letters.getFirst();
    cout << "\nThe second letter is: " << letters.getSecond();

    cout << endl;
    system("Pause");
    return 0;
}

Class .cpp file

#include "pair.h"

template <class T>
pair<T>::pair(const T& first, const T& second)
{
    f = first;
    s = second;
}

template<class T>
T pair<T>::getFirst() const
{
    return first;
}

template<class T>
T pair<T>::getSecond() const
{
    return second;
}

template<class T>
void pair<T>::setFirst(const T& first, const T& second)
{
    T temp = first;
    first = second;
    second = temp;
}

header file

template <class T>
class pair
{
private:
    T f;
    T s;
public:
    pair(const T&, const T&);
    T getFirst() const;
    T getSecond() const;
    void setFirst(const T&, const T&);
};
nelson2013
  • 17
  • 1
  • 6
  • 1
    You can't separate template declarations and implementations. Put the implementation in the header file so that `driver.cpp` can generate all the code it needs in compile-time. Also, `getFirst()` and `getSecond()` are returning non-members (I think you meant `f` and `s`). – Suedocode Apr 10 '14 at 01:57
  • Also, it is useful to post the whole error message you are getting, instead of just "C2872". – M.M Apr 10 '14 at 01:58
  • Error 1 error C2872: 'pair' : ambiguous symbol c:\users\dale nelson\documents\uvu college stuff\cs 1410 class docs\labs\lab13\lab13\source.cpp 8 1 Lab13 – nelson2013 Apr 10 '14 at 01:59

2 Answers2

1

There is already std::pair. Since you also go using namespace std;, the issue could be that compiler cannot tell whether you mean std::pair or your pair.

To fix this, either stop doing using namespace std; , or call your pair something else. Preferably both!

M.M
  • 138,810
  • 21
  • 208
  • 365
1

First, delete pair.cpp because templates can't be defined in separate source files. Next, replace pair.h with this concatenated source from pair.cpp.

Pair.h

namespace notstd {

template <class T>
class pair
{
private:
    T f;
    T s;
public:
    pair(const T&, const T&);
    T getFirst() const;
    T getSecond() const;
    void setFirst(const T&, const T&);
};

template <class T>
pair<T>::pair(const T& first, const T& second)
{
    f = first;
    s = second;
}

template<class T>
T pair<T>::getFirst() const
{
    //return first; //possible error?
    return f;
}

template<class T>
T pair<T>::getSecond() const
{
    //return second; //possible error?
    return s;
}

template<class T>
void pair<T>::setFirst(const T& first, const T& second)
{
    T temp = first;
    first = second;
    second = temp;
}

} //end namespace notstd

Finally, delete using namespace std and add using notstd::pair, OR prepend all pair with notstd::pair.

Community
  • 1
  • 1
Suedocode
  • 2,504
  • 3
  • 23
  • 41