Niepoprawne użycie operatora <<

0

co jest źle w tym kodzie ?

using namespace std;

template <class t>
class Test
{
    int liczba;
public:
    Test(t l):liczba(l){}
    friend ostream &operator<<(ostream &stream, Test<t> &obj);
};

template <class t>
ostream &operator<<(ostream &stream, Test<t> &obj)
{
    stream << obj.liczba << endl;

    return stream;
}

int main()
{
    Test<int> myObj(25);

    cout << myObj;

    return 0;
}

kompilator zgłasza błąd

undefined reference to operator<<(std::ostream&, Test<int>&)

0

A co to ma robić w ogóle?

6

Czy z bardziej rozwiniętym ostrzeżeniem już widzisz, czy potrzebujesz dalszego nakierowywania?

skasujmnie.cpp:11:61: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Test<t>&)’ declares a non-template function [-Wnon-template-friend]
   11 |     friend ostream &operator<<(ostream &stream, Test<t> &obj);
      |                                                             ^
skasujmnie.cpp:11:61: note: (if this is not what you intended, make sure the function template has already been declared and add ‘<>’ after the function name here)
/usr/bin/ld: /tmp/ccg0EwWO.o: in function `main':
skasujmnie.cpp:(.text+0x3a): undefined reference to `operator<<(std::ostream&, Test<int>&)'
6

Brakuje template

template <class t>
class Test
{
    int liczba;
public:
    Test(t l):liczba(l){}

    template<typename U>
    friend ostream &operator<<(ostream &stream, Test<U> &obj);
};

https://godbolt.org/z/zh45EYPWb

Generalnie ty zadeklarowałeś przyjaźń z funkcją, a potrzebujesz przyjaźń z szablonem funkcji.

0
Althorion napisał(a):

Czy z bardziej rozwiniętym ostrzeżeniem już widzisz, czy potrzebujesz dalszego nakierowywania?

skasujmnie.cpp:11:61: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Test<t>&)’ declares a non-template function [-Wnon-template-friend]
   11 |     friend ostream &operator<<(ostream &stream, Test<t> &obj);
      |                                                             ^
skasujmnie.cpp:11:61: note: (if this is not what you intended, make sure the function template has already been declared and add ‘<>’ after the function name here)
/usr/bin/ld: /tmp/ccg0EwWO.o: in function `main':
skasujmnie.cpp:(.text+0x3a): undefined reference to `operator<<(std::ostream&, Test<int>&)'

nie wiem jak to zrobiłeś ale u mnie inaczej kompilator gada

zabawa2/main.cpp:36: undefined reference to `operator<<(std::ostream&, Test<int>&)'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:71: debug/zabawa2.exe] Error 1
mingw32-make[1]: Leaving directory 'C:/Projekty-Qt/konsolowki/zabawa2/zabawa2-Debug'
mingw32-make: *** [Makefile:45: debug] Error 2
17:57:14: Proces "C:\Qt\Qt-6.3.0\Tools\mingw1120_64\bin\mingw32-make.exe" zakończył się kodem wyjściowym 2.
Błąd budowania / instalowania projektu zabawa2 (zestaw narzędzi: Desktop Qt 6.3.0 MinGW 64-bit)
Podczas wykonywania kroku "Make"
0

Czemu ty zawsze sobie tak strasznie komplikujesz?

#include <iostream>
using namespace std;

template<class T> class Test
{
    T value;
	public:
    Test(const T &value):value(value) {}
    private:
    ostream &prn(ostream &stream) const;
    friend ostream &operator<<(ostream &s,const Test<T> &tst) { return tst.prn(s); }
};

template<class T> ostream &Test<T>::prn(ostream &s) const { return s<<value<<endl; }

int main()
{
    Test<int> myObj(25);
    cout<<myObj;
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1