-2
ostreamとistreamをテンプレートのクラスのfreindとしてオーバーロードしようとしています。私はオンラインで見てきましたが、テンプレートのオーバーロードについて指定したものをあまり見つけられていませんでした。また、私が見てきたことは、これをorevloadする方法です。私はプログラミングにとても慣れていて、何か助けに感謝していることは明らかです。ありがとうございました。テンプレートとしての挿入演算子と抽出演算子のオーバーロード
#include <stdio.h>
#include<vector>
#include<iostream>
using namespace std;
template<class T>
class MyClass
{
enter code here
public:
MyClass();
MyClass(const T& p_val1);
MyClass(const MyClass<T>& p_val1);
~MyClass();
MyClass<T>& operator=(MyClass<T>& rhs);
friend ostream& operator<<(ostream& lhs, const MyClass<T> &printme);
friend istream& operator>><T>(istream& lhs, MyClass<T>& readme);
private:
T* m_val1;
}
ostreamとistreamの実装。ここ
template<class T>
ostream& operator<<(ostream&lhs, const MyClass<T>& printme)
{
lhs << printme.m_val1;
return lhs;
}
template<class T>
istream& operator>>(istream& lhs, MyClass<T>& readme)
{
lhs >> *(readme.m_val1);
return lhs;
}
エラー
Undefined symbols for architecture x86_64:
"MyClass<int>::~MyClass()", referenced from:
_main in main.o
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, MyClass<int> const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
主な機能
MyClass<int> a(8);
MyClass<int> b(9);
cout << " Enter a value for the two types to be swapped: ";
cin >> a >> b;
cout << "Before swapping...\n";
cout << "Object a's pointer member dereferences a value of:\t" << a << endl;
cout << "Object b's pointer member dereferences a value of:\t" << b << endl;
コンパイルする実際のコードを投稿してください。 –