请问高手这是个什么问题,怎么解决(100分)

  • 主题发起人 主题发起人 pingping2
  • 开始时间 开始时间
P

pingping2

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大哥大姐,小妹初学c++builder,遇到一个问题,显示为:
[Linker Error] Unresolved external 'operator >>(_STL::basic_istream<char, _STL::char_traits<char> >&amp;, SparseMatrix<int>&amp;)' referenced from D:/MP3/C4/SMATRIX.OBJ
请问各位大哥大姐,这个问题怎么解决,小妹不胜感激!!!
 
C++ Builder ?好久没用了,支持你一下。
 
把代码贴出来啊!不然神仙也帮不了你!
 
连接错误,可能是相应的库没有添加,添加相关的*.lib就可以了
 
代码为:
#include <iostream.h>
#include "smatrix.h"
#pragma link "smatrix"
void main(void)
{
SparseMatrix<int> A(20), B(20), C(20);
cin >> A;
cout << "Matrix A is" << endl << A;
cin >> B;
cout << "Matrix B is" << endl << B;
A.Transpose(C);
cout << "The transpose of A is" << endl << C;
A.Add(B,C);
cout << "The sum of A and B is" << endl << C;
 
在void main()前加入
using namespace std;
 
SparseMatrix函数没有定义函数体,只有头文件的声明。请查看SparseMatrix在哪里定义的。
 
你的SparseMatrix模板类有没有重载算符“>>和<<”?如果没有的话,cin>>A、cin>>B,cout<<B、cout<<C是无法通过编译的,另外还得加上PoeChan说的:using namespace std;
 
各位前辈,还是不行,问题依旧。
还有,我将build with runtime packages 前的勾去掉再打上,结果又出现了一个新问题:
[C++ Warning] xcept.h(23): W8058 Cannot create pre-compiled header: code in header。还请高手指教!
 
sparsemtrix.h的代码如下:
#define SparseMatrix_
#include <stdlib.h>
#include <iostream.h>
#include "term.h"
#include "xcept.h"
using namespace std;
template<class T>
class SparseMatrix
{
friend ostream&amp;
operator<<
(ostream&amp;, const SparseMatrix<T>&amp;);
friend istream&amp;
operator>>
(istream&amp;, SparseMatrix<T>&amp;);
public:
SparseMatrix(int maxTerms = 10);
~SparseMatrix() {delete [] a;}
void Transpose(SparseMatrix<T> &amp;b) const;
void Add(const SparseMatrix<T> &amp;b,
SparseMatrix<T> &amp;c) const;
private:
void Append(const Term<T>&amp;
t);
int rows, cols;
// matrix dimensions
int terms;
// current number of nonzero terms
Term<T> *a;
// term array
int MaxTerms;
// size of array a;
};
template<class T>
SparseMatrix<T>::SparseMatrix(int maxTerms)
{// Sparse matrix constructor.
if (maxTerms < 1) throw BadInitializers();
MaxTerms = maxTerms;
a = new Term<T> [MaxTerms];
terms = rows = cols = 0;
}
// overload <<
template <class T>
ostream&amp;
operator<<(ostream&amp;
out,
const SparseMatrix<T>&amp;
x)
{// Put *this in output stream.
// put matrix characteristics
out << "rows = " << x.rows << " columns = "
<< x.cols << endl;
out << "nonzero terms = " << x.terms << endl;
// put terms, one per line
for (int i = 0;
i < x.terms;
i++)
out << "a(" << x.a.row << ',' << x.a.col
<< ") = " << x.a.value << endl;
return out;
}
// overload >>
template<class T>
istream&amp;
operator>>(istream&amp;
in, SparseMatrix<T>&amp;
x)
{// Input a sparse matrix.
// input matrix characteristics
cout << "Enter number of rows, columns, and terms"
<< endl;
in >> x.rows >> x.cols >> x.terms;
if (x.terms > x.MaxTerms) throw NoMem();
// input terms
for (int i = 0;
i < x.terms;
i++) {
cout << "Enter row, column, and value of term "
<< (i + 1) << endl;
in >> x.a.row >> x.a.col >> x.a.value;
}
return in;
}
template<class T>
void SparseMatrix<T>::
Transpose(SparseMatrix<T> &amp;b) const
{// Return transpose of *this in b.
// make sure b has enough space
if (terms > b.MaxTerms) throw NoMem();
// set transpose characteristics
b.cols = rows;
b.rows = cols;
b.terms = terms;
// initialize to compute transpose
int *ColSize, *RowNext;
ColSize = new int[cols + 1];
RowNext = new int[rows + 1];

// find number of entries in each column of *this
for (int i = 1;
i <= cols;
i++) // initialize
ColSize = 0;

for (int i = 0;
i < terms;
i++)
ColSize[a.col]++;

// find the starting point of each row of b
RowNext[1] = 0;
for (int i = 2;
i <= cols;
i++)
RowNext = RowNext[i - 1] + ColSize[i - 1];


// perform the transpose copying from *this to b
for (int i = 0;
i < terms;
i++) {
int j = RowNext[a.col]++;
// position in b
b.a[j].row = a.col;
b.a[j].col = a.row;
b.a[j].value = a.value;
}
}
template<class T>
void SparseMatrix<T>::Append(const Term<T>&amp;
t)
{// Append a nonzero term t to *this.
if (terms >= MaxTerms) throw NoMem();
a[terms] = t;
terms++;
}
template<class T>
void SparseMatrix<T>::Add(const SparseMatrix<T> &amp;b,
SparseMatrix<T> &amp;c) const
{// Compute c = (*this) + b.
// verify compatibility
if (rows != b.rows || cols != b.cols)
throw SizeMismatch();
// incompatible matrices
// set characteristics of result c
c.rows = rows;
c.cols = cols;
c.terms = 0;
// initial value
// define cursors to move through *this and b
int ct = 0, cb = 0;
// move through *this and b adding like terms
while (ct < terms &amp;&amp;
cb < b.terms) {
// Row-major index plus cols of each term
int indt = a[ct].row * cols + a[ct].col;
int indb = b.a[cb].row * cols + b.a[cb].col;
if (indt < indb) {// b term comes later
c.Append(a[ct]);
ct++;} // next term of *this
else
{if (indt == indb) {// both in same position
// append to c only if sum not zero
if (a[ct].value + b.a[cb].value) {
Term<T> t;
t.row = a[ct].row;
t.col = a[ct].col;
t.value = a[ct].value + b.a[cb].value;
c.Append(t);}
ct++;
cb++;} // next terms of *this and b
else
{c.Append(b.a[cb]);
cb++;} // next term of b
}
}
// copy over remaining terms
for (;
ct < terms;
ct++)
c.Append(a[ct]);
for (;
cb < b.terms;
cb++)
c.Append(b.a[cb]);
}
#endif
 
xcept.h代码如下啊:
#ifndef Xcept_
#define Xcept_
#include <except.h>
#include <new.h>
// bad initializers
class BadInitializers {
public:
BadInitializers() {}
};
// insufficient memory
class NoMem {
public:
NoMem() {}
};
// change new to throw NoMem instead of xalloc
void my_new_handler()
{
throw NoMem();
};
new_handler Old_Handler_ = set_new_handler(my_new_handler);
// improper array, find, insert, or delete index
// or deletion from empty structure
class OutOfBounds {
public:
OutOfBounds() {}
};
// use when operands should have matching size
class SizeMismatch {
public:
SizeMismatch() {}
};
// use when zero was expected
class MustBeZero {
public:
MustBeZero() {}
};
// use when zero was expected
class BadInput {
public:
BadInput() {}
};
#endif
 
关于矩阵,TechSoft有一个免费的文件,其具备矩阵的基本功能,参考一下,或许对你有帮助,网址好像是:www.techsoft.com
 
虽然Term文件中可能只是定义一个数组,但是你的全部文件没有贴出来,在注释中有一些问题,看看提法是否对,
template<class T>
SparseMatrix<T>::SparseMatrix(int maxTerms)
{// Sparse matrix constructor.
if (maxTerms < 1) throw BadInitializers();
MaxTerms = maxTerms;
a = new Term<T> [MaxTerms];
terms = rows = cols = 0;
//这一句表明矩阵的行、列、元素的项数都设置为0,不知道是不是你的本意,我知道
//普通矩阵一般不是这样初始化的,稀疏矩阵似乎也不应该这样
}
void main(void)
{
SparseMatrix<int> A(20), B(20), C(20);
//根据构造函数,A,B,C,的terms,col,row都是0,因此,下面都得不到输出
cin >> A;
cout << "Matrix A is" << endl << A;
cin >> B;
cout << "Matrix B is" << endl << B;
A.Transpose(C);
cout << "The transpose of A is" << endl << C;
A.Add(B,C);
cout << "The sum of A and B is" << endl << C;
}
 
高手帮忙啊
 
急需期待正确的解答!
 
你的悉数矩阵的构造函数,没有行和烈的参数,这样改一下,看看如何,我没有试验
在声明部分: SparseMatrix(int RowNum,int ColNum,int maxTerms = 10);
在实现部分:
template<class T>
SparseMatrix<T>::(int RowNum,int ColNum,int maxTerms = 10)
{// Sparse matrix constructor.
if (maxTerms < 1) throw BadInitializers();
MaxTerms = maxTerms;
a = new Term<T> [MaxTerms];
//terms = rows = cols = 0;
rows = RowNum;
cols = ColNum;
//不知道在程序中terms代表什么,使所有的项得多少还是不等于0的项的多少,如果是//所有项的多少,我认为多余, 如果是不等于0的项的多少,那么还要改
}
改成如下
在声明部分: SparseMatrix(int RowNum,int ColNum,int TermNum,int maxTerms = 10);
在实现部分:
template<class T>
SparseMatrix<T>::(int RowNum,int ColNum,int TermNum,int maxTerms = 10)
{// Sparse matrix constructor.
if (maxTerms < 1) throw BadInitializers();
MaxTerms = maxTerms;
a = new Term<T> [MaxTerms];
//terms = rows = cols = 0;
rows = RowNum;
cols = ColNum;
terms = TermNum;
}
 
后退
顶部