c++程序改错
无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Array1D<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Array1D@H@@@Z),该符号在函数 _main 中被引用
代码如下:
#ifndef ARRAY1D_CLASS
#define ARRAY1D_CLASS
#include <assert.h>
#include<iostream>
using namespace std;
template <class T>
class Array1D
{
public:
Array1D(int sz=0);
~Array1D(){ delete []elements; }
T& operator [](int i)const;
Array1D<T>&operator=(const Array1D<T> &r);
void InputMatrix(T** a,int n);
void OutputMatrix();
friend istream &operator>>(istream &in, Array1D<T> &r);
friend ostream &operator<<(ostream &out, const Array1D<T> &r);
private:
int size;
T *elements;
};
#endif//ARRAY1D_CLASS
template <class T>
Array1D<T>::Array1D(int sz)
{
assert(sz>=0);
size=sz;
elements=new T[sz];
}
template <class T>
void Array1D<T>::InputMatrix(T **a,int n)
{
a=new T*[n];
for(int i=0;i<n;i++)
a[i]=new T[n];
int i,j,k=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
cin>>a[i][j];
if(i<=j){
elements[k]=a[i][j];k++;size++;}
}
delete a;a=NULL;
}
template <class T>
void Array1D<T>::OutputMatrix()
{
for(i=0;i<n;i=i*(i+1)/2){
cout<<elements[i];
}
}
template <class T>
T& Array1D<T>::operator [](int i)const
{
assert(i>=0&&i<size);
return elements[i];
}
template <class T>
Array1D<T>& Array1D<T>::operator=(const Array1D<T> &r)
{
if(this!=&r) {
size=r.size;
delete []elements;
elements=new T[size];
for(int i=0; i<size; i++)
elements[i]=r.elements[i];
}
return *this;
}
template <class T>
istream &operator>>(istream &in, Array1D<T> &r)
{
cout<<"Intput array\n";
for(int i=0; i<r.size; i++) in>>r.elements[i];
return in;
}
template <class T>
ostream &operator<<(ostream &out, const Array1D<T> &r)
{
cout<<"Array=";
for(int i=0; i<r.size; i++) out<<r.elements[i]<<' ';
out<<endl;
return out;
}
#include<iostream>
using namespace std;
#include"Array1D.h"
int main()
{
Array1D<int> a(6);int **p=NULL;
a.InputMatrix(p,3);
cout<<a;
}
答案:这个错误是由于编译器不支持模板函数的声明与定义分离造成的,把定义放到类声明里即可:#ifndef ARRAY1D_CLASS
#define ARRAY1D_CLASS
#include <assert.h>
#include<iostream>
using namespace std;
template <class T>
class Array1D
{
public:
Array1D(int sz=0);
~Array1D(){ delete []elements; }
T& operator [](int i)const;
Array1D<T>&operator=(const Array1D<T> &r);
void InputMatrix(T** a,int n);
void OutputMatrix();
friend istream &operator>>(istream &in, Array1D<T> &r)
{
cout<<"Intput array\n";
for(int i=0; i<r.size; i++) in>>r.elements[i];
return in;
}
friend ostream &operator<<(ostream &out, const Array1D<T> &r)
{
cout<<"Array=";
for(int i=0; i<r.size; i++) out<<r.elements[i]<<' ';
out<<endl;
return out;
}
private:
int size;
T *elements;
};
#endif//ARRAY1D_CLASS
在vs 2008里编译能通过
VC6.0对模板支持不好,建议更换~~
其实类模版有两种编译模型,一种叫分开编译,一种叫包含编译,你可以去网上搜下关于这两种编译模型。但是在windows下几乎所有的编译器都不支持两种编译模型,所以头文件和实现文件不能分开,只能放在同一个文件里面,但是你的代码就算放同一个文件里面也会出现上述错误,主要是因为你的类声明中有两个函数声明错误template<class T>
friend istream &operator>> <T>(paralist);template<class T>friend ostream &operator<< <T>(paralist);
下面是我给你改的,你仔细看看#ifndef ARRAY1D_CLASS
#define ARRAY1D_CLASS
#include <assert.h>
#include<iostream>
using namespace std;
template <class T>
class Array1D
{
public:
Array1D(int sz=0);
~Array1D(){ delete []elements; }
T& operator [](int i)const;
Array1D<T>&operator=(const Array1D<T> &r);
void InputMatrix(T** a,int n);
void OutputMatrix();
friend istream &operator>> <T>(istream &in, Array1D<T> &r);
friend ostream &operator<< <T>(ostream &out, const Array1D<T> &r);
private:
int size;
T *elements;
};
template <class T>
Array1D<T>::Array1D(int sz)
{
assert(sz>=0);
size=sz;
elements=new T[sz];
}
template <class T>
void Array1D<T>::InputMatrix(T **a,int n)
{
a=new T*[n];
for(int i=0;i<n;i++)
a[i]=new T[n];
int i,j,k=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
cin>>a[i][j];
if(i<=j){
elements[k]=a[i][j];k++;size++;}
}
delete a;
a=NULL;
}
template <class T>
void Array1D<T>::OutputMatrix()
{
for(i=0;i<n;i=i*(i+1)/2){
cout<<elements[i];
}
}
template <class T>
T& Array1D<T>::operator [](int i)const
{
assert(i>=0&&i<size);
return elements[i];
}
template <class T>
Array1D<T>& Array1D<T>::operator=(const Array1D<T> &r)
{
if(this!=&r) {
size=r.size;
delete []elements;
elements=new T[size];
for(int i=0; i<size; i++)
elements[i]=r.elements[i];
}
return *this;
}
template <class T> inline
istream &operator>>(istream &in, Array1D<T> &r)
{
cout<<"Intput array\n";
for(int i=0; i<r.size; i++)
in >> r.elements[i];
return in;
}
template <class T> inline
ostream &operator<<(ostream &out, const Array1D<T> &r)
{
out<<"Array=";
for(int i=0; i<r.size; i++)
out<< r.elements[i] << ' ';
out<<endl;
return out;
}
#endif
要想实现两种编译模型,可以在Linux环境中进行C++编程,用GNU编译器
上一个:C++提取网页信息
下一个:C++语言是什么?