并提前感谢您的帮助。我是C / C ++的新手,正在通过把自己投入到深层来教自己。我试图写一个共享(静态?不确定差异)库并编写一个简单的程序来使用所述库。我可能不是以最有效的方式做到这一点(如果它是Python,我将在一周前完成这个项目)但我更关心的是知道什么以及为什么我做错了。再次感谢您的帮助:
LinAlg.h
//// PROTECT HEADER
#pragma once
//// IMPORT LIBRARIES
// standard modules
#include <exception>
// custom modules
//// DEFINE OBJECT
namespace LinAlg
{
class ktaArr
{
public:
// INIT
ktaArr(int w, int h);
ktaArr(int w, int h, double val);
// SETTERS
void SetValue(int row, int col, double val);
// GETTERS
int GetWidth();
int GetHeight();
double GetValue(int row, int col);
// METHODS
ktaArr dot(ktaArr arr2);
void Display();
// OPPERATORS
ktaArr operator=(ktaArr arr2);
ktaArr operator+(ktaArr arr2);
ktaArr operator-(ktaArr arr2);
ktaArr operator*(ktaArr arr2);
ktaArr operator/(ktaArr arr2);
// RELATIONS
bool operator==(ktaArr arr2);
bool operator<=(ktaArr arr2);
bool operator>=(ktaArr arr2);
bool operator!=(ktaArr arr2);
// DECONSTRUCTOR
~ktaArr()
{
for(int i = 0; i < h; i++)
{
delete arr[i];
}
};
protected:
int w, h;
double** arr;
private:
};
}
//// DEFINE EXCEPTION
namespace LinAlg
{
class IllegalArraySize: public std::exception {
private:
virtual const char* what() const throw()
{
return "Invalid use: array sizes do not match.";
}
};
}
LinAlg.cpp
//// IMPORT LIBRARIES
// standard modules
#include <iostream>
// custom modules
#include "LinAlg.h"
///////////////////////////////////////////////////////////////////////////////
// INIT
LinAlg::ktaArr::ktaArr(int h, int w)
{
LinAlg::ktaArr::w = w;
LinAlg::ktaArr::h = h;
// LinAlg::ktaArr::arr = new double[LinAlg::ktaArr::h][LinAlg::ktaArr::w];
LinAlg::ktaArr::arr = new double*[LinAlg::ktaArr::h];
for (int col = 0; col < LinAlg::ktaArr::w; col++)
{
LinAlg::ktaArr::arr[col] = new double[LinAlg::ktaArr::w];
}
}
LinAlg::ktaArr::ktaArr(int h, int w, double val)
{
LinAlg::ktaArr::w = w;
LinAlg::ktaArr::h = h;
// LinAlg::ktaArr::arr = new double[LinAlg::ktaArr::h][LinAlg::ktaArr::w];
LinAlg::ktaArr::arr = new double*[LinAlg::ktaArr::h];
for (int col = 0; col < LinAlg::ktaArr::w; col++)
{
LinAlg::ktaArr::arr[col] = new double[LinAlg::ktaArr::w];
}
//iterate over array and set values to val
for (int row = 0; row < LinAlg::ktaArr::h; row++)
{
for (int col = 0; col < LinAlg::ktaArr::w; col++)
{
LinAlg::ktaArr::arr[row][col] = val;
}
}
}
// INIT
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// SETTERS
void LinAlg::ktaArr::SetValue(int row, int col, double val)
{
if ((row+1 > LinAlg::ktaArr::h) || (col+1 > LinAlg::ktaArr::w))
{
throw LinAlg::IllegalArraySize();
}
LinAlg::ktaArr::arr[row][col] = val;
}
// SETTERS
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// GETTERS
int LinAlg::ktaArr::GetWidth()
{
return LinAlg::ktaArr::w;
}
int LinAlg::ktaArr::GetHeight()
{
return LinAlg::ktaArr::h;
}
double LinAlg::ktaArr::GetValue(int row, int col)
{
return LinAlg::ktaArr::arr[row][col];
}
// GETTERS
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// METHODS
LinAlg::ktaArr LinAlg::ktaArr::dot(LinAlg::ktaArr arr2)
{
// Check size of arrays first
if (LinAlg::ktaArr::h != arr2.GetHeight())
{
throw LinAlg::IllegalArraySize();
}
// Create new array
LinAlg::ktaArr arrNew(LinAlg::ktaArr::h, arr2.GetWidth());
// Assign each value
double value;
for (int row = 0; row < arrNew.GetHeight(); row++)
{
for (int col = 0; col < arrNew.GetWidth(); col++)
{
value = 0;
// Perform multiplication
for (int el = 0; el < w; el++)
{
value += LinAlg::ktaArr::arr[row][el] *
arr2.GetValue(el, col);
}
arrNew.SetValue(row, col, value);
}
}
return arrNew;
}
void LinAlg::ktaArr::Display()
{
for (int row = 0; row < LinAlg::ktaArr::h; row++)
{
for (int col = 0; col < LinAlg::ktaArr::w; col++)
{
if ((row == 0) && (col == 0))
{
// first element
std::cout << "[[" << LinAlg::ktaArr::arr[row][col];
}
else if ((row == LinAlg::ktaArr::h-1) &&
(col == LinAlg::ktaArr::w-1))
{
// last element
std::cout << ", " << LinAlg::ktaArr::arr[row][col]
<< "]]" << std::endl;
}
else if ((row != 0) && (col == 0))
{
// first element of row
std::cout << " [" << LinAlg::ktaArr::arr[row][col];
}
else if ((row != LinAlg::ktaArr::h-1) &&
(col == LinAlg::ktaArr::w-1))
{
// last element of row
std::cout << ", " << LinAlg::ktaArr::arr[row][col]
<< "]" << std::endl;
}
else
{
// print out value
std::cout << ", " << LinAlg::ktaArr::arr[row][col];
}
}
}
}
// METHODS
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// OPPERATORS
LinAlg::ktaArr LinAlg::ktaArr::operator=(LinAlg::ktaArr arr2)
{
LinAlg::ktaArr newArr = LinAlg::ktaArr(arr2.GetHeight(),
arr2.GetWidth());
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
newArr.SetValue(row, col, arr2.GetValue(row,col));
}
}
return newArr;
}
LinAlg::ktaArr LinAlg::ktaArr::operator+(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
LinAlg::ktaArr newArr = LinAlg::ktaArr(arr2.GetHeight(),
arr2.GetWidth());
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
newArr.SetValue(row, col,
LinAlg::ktaArr::arr[row][col]
+ arr2.GetValue(row,col));
}
}
return newArr;
}
LinAlg::ktaArr LinAlg::ktaArr::operator-(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
LinAlg::ktaArr newArr = LinAlg::ktaArr(arr2.GetHeight(),
arr2.GetWidth());
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
newArr.SetValue(row, col,
LinAlg::ktaArr::arr[row][col]
- arr2.GetValue(row,col));
}
}
return newArr;
}
LinAlg::ktaArr LinAlg::ktaArr::operator*(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
LinAlg::ktaArr newArr = LinAlg::ktaArr(arr2.GetHeight(),
arr2.GetWidth());
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
newArr.SetValue(row, col,
LinAlg::ktaArr::arr[row][col]
* arr2.GetValue(row,col));
}
}
return newArr;
}
LinAlg::ktaArr LinAlg::ktaArr::operator/(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
LinAlg::ktaArr newArr = LinAlg::ktaArr(arr2.GetHeight(),
arr2.GetWidth());
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
newArr.SetValue(row, col,
LinAlg::ktaArr::arr[row][col]
/ arr2.GetValue(row,col));
}
}
return newArr;
}
// OPPERATORS
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// RELEATIONS
bool LinAlg::ktaArr::operator==(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
if (LinAlg::ktaArr::arr[row][col] != arr2.GetValue(row, col))
{
return false;
}
}
}
return true;
}
bool LinAlg::ktaArr::operator<=(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
if (LinAlg::ktaArr::arr[row][col] >= arr2.GetValue(row, col))
{
return false;
}
}
}
return true;
}
bool LinAlg::ktaArr::operator>=(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
if (LinAlg::ktaArr::arr[row][col] <= arr2.GetValue(row, col))
{
return false;
}
}
}
return true;
}
bool LinAlg::ktaArr::operator!=(LinAlg::ktaArr arr2)
{
if ((LinAlg::ktaArr::h != arr2.GetHeight()) ||
(LinAlg::ktaArr::w != arr2.GetWidth()))
{
throw LinAlg::IllegalArraySize();
}
for(int row = 0; row < arr2.GetHeight(); row++)
{
for(int col = 0; col < arr2.GetWidth(); col++)
{
if (LinAlg::ktaArr::arr[row][col] == arr2.GetValue(row, col))
{
return false;
}
}
}
return true;
}
// RELATIONS
///////////////////////////////////////////////////////////////////////////////
的main.cpp
//// IMPORT LIBRARIES
// standard modules
#include <iostream>
// custom modules
#include "../LinAlgLib/LinAlg.h"
int main()
{
LinAlg::ktaArr identity (3,3,0.0);
std::cout << "Display 3x3 zeros array:" << std::endl;
identity.Display();
std::cout << "test" << std::endl;
return 0;
}
一切都在Mac OSX 10.9上完成,使用GCC如下:
LinAlg图书馆
make all
Building file: ../LinAlg.cpp
Invoking: Cross G++ Compiler
g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"LinAlg.d" -MT"LinAlg.d" -o "LinAlg.o" "../LinAlg.cpp"
Finished building: ../LinAlg.cpp
Building target: libLinAlg.so
Invoking: Cross G++ Linker
g++ -shared -o "libLinAlg.so" ./LinAlg.o
Finished building target: libLinAlg.so
测试应用(main.cpp):
make all
Building file: ../main.cpp
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: testKyleLinAlg
Invoking: Cross G++ Linker
g++ -o "testKyleLinAlg" ./main.o
Undefined symbols for architecture x86_64:
"LinAlg::ktaArr::ktaArr(int, int, double)", referenced from:
_main in main.o
"LinAlg::ktaArr::Display()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [testKyleLinAlg] Error 1
再次感谢您的帮助。