• 締切済み

自作の行列クラスを継承するさいにエラーがでます

noconanの回答

  • noconan
  • ベストアンサー率0% (0/1)
回答No.2

自分も学習のみなのですが、感想をいいますね。 おそらくNo1さんの指摘したとおりだと思います。 解決策として、基底クラスで派生クラスをインスタンス化すれば解決できるかもしれません。 ちょっと気になったこと: 派生クラスに引数が3つのコンストラクタがないのですが・・・・・lduMatrix a(3,3, 3.14); とするには必要ですよね・・・・ ここに掲示していないところで記述してあるってことかな? 間違ってたらごめんなさい・・・・・

carnot
質問者

お礼

コメント、ありがとうございます。 基底クラスの方の省略した部分に引数3のコンストラクタがありますので、こちらを使用してlduMatrix(3,3,3.14)を実現しています。

関連するQ&A

  • クラスの合成ができない

    大学で数値計算をしており、Matrixクラスを作ったのですが他のクラスや構造体のメンバ変数に取り入れようとしてもできません。 以下のようなコードなのですがどうしてできないのでしょう? #include<iostream> using namespace std; class Matrix{ private: double **ptr; int Row,Col; public: Matrix(int i=1, int j=1);/* コンストラクタ */ Matrix(const Matrix &);/* コピーコンストラクタ */ ~Matrix();/* デストラクタ */ }; Matrix::Matrix(int Dim1,int Dim2) { Row=Dim1; Col=Dim2; for(int i=0;i<=Dim1;i++){ ptr[i]=(double*) new double[Dim2+1]; if(ptr[i]==NULL){ cerr<<"Memory Allocation Error.\n"; abort(); } } } Matrix::~Matrix() { for(int i=1;i<=Row;i++)delete [] ptr[i]; delete [] ptr; ptr=NULL; } struct Test{ Matrix A(3,3);//これはエラー }; int main() { Matrix B(3,3);//これは出来た return 0; }

  • エラーの原因が分かりません "undefined reference to"

    ライブラリのヘッダファイル matrix.h #ifndef MATRIX_H #define MATRIX_H #ifdef __cplusplus extern "C"{ #endif typedef struct{ int row,col; double *elements; } MATRIX; extern int matrix_error_code; extern void zero_matrix(MATRIX *a); extern void identity_matrix(MATRIX *a); extern void show_matrix(MATRIX *x); extern MATRIX multiply_matrix(MATRIX *a,MATRIX *b); extern MATRIX transposed_matrix(MATRIX *a); #ifdef __cplusplus } #endif #endif ライブラリのソースファイル matrix.c #include <stdio.h> #include <stdlib.h> #include <math.h> #include "matrix.h" int matrix_error_code = 0; void zero_matrix(MATRIX *a) { int i,size; double *p; size=a->row*a->col; if((p = malloc(sizeof(double)*size)) == NULL) { puts("メモリを確保できません."); exit(0); } a->elements=p; for(i=0;i<size;i++) *p++=0; } void identity_matrix(MATRIX *a) { int i; zero_matrix(a); for(i=0;i<a->row;i++) { *(a->elements+i*a->col+i)=1.0000; } } void show_matrix(MATRIX *x) { int i,j; for(i=0;i<x->row;i++) { for(j=0;j<x->col;j++) { printf("%3.4lf ",*(x->elements+i*x->col+j)); } printf("\n"); } printf("\n"); } MATRIX transposed_matrix(MATRIX *a) { MATRIX x; int i,j; double *p; if((p = malloc(sizeof(double)*(a->row*a->col))) == NULL) { puts("メモリを確保できません."); exit(0); } x.elements=p; x.row=a->col; x.col=a->row; for(i=0;i<x.row;i++) { for(j=0;j<x.col;j++) { *p++=*(a->elements+j*x.row+i); } } return x; } MATRIX multiply_matrix(MATRIX *a,MATRIX *b) { int i,j,k; double *p; MATRIX x; x.row=a->row; x.col=b->col; if((p = malloc(sizeof(double)*(x.row*x.col))) == NULL) { puts("メモリを確保できません."); exit(0); } x.elements=p; for(i=0;i<x.row;i++) { for(j=0;j<x.col;j++) { for(k=0;k<a->col;k++) { *p += *(a->elements+i*a->col+k) * *(b->elements+k*b->row+j); } } p++; } return x; } 動作確認のアプリケーションファイル test_matrix.c #include <stdio.h> #include "matrix.h" int main(void) { int i,j; MATRIX b,c,d,f; b.row=c.row=2; b.col=c.col=2; identity_matrix(&b); identity_matrix(&c); d=transposed_matrix(&b); show_matrix(&d); f=multiply_matrix(&b,&c); show_matrix(&f); return 0; } ライブラリのコンパイルまではエラーなしで出来るのですが、test_matrix.cをコンパイルすると、"undefined reference to '_transposed_matrix'"と、"undefined reference to '_multiply_matrix'"の2つが表示されてコンパイルができません。 どうかよろしくお願いします。

  • 行列の積を計算するプログラムがうまくいきません

    どこが間違っているのかわかる方お願いします ・行列A,Bはファイルから読み込む ・行列A,Bの積Cの計算には関数を用いる #include<stdio.h> #define ROW 10 #define COL 10 void MatrixProduct(int a[][COL],int b[][ROW],int c[][ROW],int n,int m ) { int i,j,k; for(i=0;i<n;i++){ for(j=0;j<n;j++){ c[i][j]=0; } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ for(k=0;k<m;k++){ c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } } int main(void) { FILE *fp1,*fp2; char fname1[64],fname2[64]; int a[ROW][COL],b[ROW][COL],c[ROW][COL],n,m; int i,j,k; printf("Input file name ?"); scanf("%s",fname1); printf("Output file name ?"); scanf("%s",fname2); fp1=fopen(fname1,"r"); fp2=fopen(fname2,"w"); fscanf(fp1,"%d %d",&n,&m); MatrixProduct(a,b,c,n,m); for(i=0;i<n;i++){ for(j=0;j<n;j++){ fprintf(fp2,"%3d",c[i][j]); } fprintf(fp2,"\n"); } fclose(fp1); fclose(fp2); return(0); } fp1 3 4 1 2 3 4 2 3 4 5 3 4 5 6 1 2 3 2 3 4 3 4 5 4 5 6

  • 整数を引数にもつテンプレートクラスについて

    テンプレート引数に整数を使えると 聞いたので以下のようなクラスを 作りました。 template <int w, int h> class MyClass { double a[w][h]; MyClass(); }; temlate <int w, int h> void MyClass<w, h>::MyClass(void) { for(int i = 0; i < w; i++) { for(int j = 0; j < h; j++) { a[i][j] = 0; } } } ここでMyClass<2, 4> mc;などと呼び出してコンパイルすると undefined reference to `Matrix<2, 4>::Matrix()` とエラーが表示されます。 何が間違っているのでしょうか?

  • ポインタの指しているアドレスは同じなのに表示される内容が違う。

    行列に関するプログラムです。 ライブラリのヘッダファイル matrix.h #ifndef MATRIX_H #define MATRIX_H #ifdef __cplusplus extern "C"{ #endif typedef struct{ int row,col; double *elements; } MATRIX; extern int matrix_error_code; extern MATRIX zero_matrix(MATRIX a); extern MATRIX identity_matrix(MATRIX a); extern void func_matrix(MATRIX a); #ifdef __cplusplus } #endif #endif ライブラリのソースファイル matrix.c #include <stdio.h> #include <stdlib.h> #include <math.h> #include "matrix.h" int matrix_error_code = 0; MATRIX zero_matrix(MATRIX a) /*零行列をつくる*/ { int i,size; double *p; size=a.row*a.col; for(i=0;i<size;i++) { if((p = malloc(sizeof(double))) == NULL) { puts("メモリを確保できません."); exit(0); } *p=0; } a.elements=p-size; return a; } MATRIX identity_matrix(MATRIX a) /*単位行列をつくる*/ { int i; a=zero_matrix(a); for(i=0;i<a.row;i++) { *(a.elements+i*a.col+i)=1.0000; } return a; } void func_matrix(MATRIX a) { int i,j; double *p; printf("b.elementsの値:%p\n",a.elements); printf("b[1,1]の値:%lf\n",a.elements); } 動作確認用のアプリケーションファイル test_matrix.c #include <stdio.h> #include "matrix.h" int main(void) { int i,j; MATRIX b; b.row=2; b.col=2; b=identity_matrix(b); for(i=0;i<b.row;i++) { for(j=0;j<b.col;j++) { printf("%3.4lf ",*(b.elements+i*b.col+j)); } printf("\n"); } printf("\n\n"); printf("b.elementsの値:%p\n",b.elements); printf("b[1,1]の値:%lf\n",b.elements); func_matrix(b); return 0; } void func_matrix(MATRIX a)内のprintf("b.elementsの値:%p\n",a.elements);とtest_matrix.c内のprintf("b.elementsの値:%p\n",b.elements);で表示される値は同じなので、main関数のbがvoid func_matrix(MATRIX a)内のaに正しく引き渡されていると思うのですが、それぞれの次の行での、printf("b[1,1]の値:%lf\n",b.elements);とprintf("b[1,1]の値:%lf\n",a.elements);とでは表示される値が違い、理由がわからず困っています。 どうか回答をよろしくお願いします。

  • c++のvirtual関数および継承について

    以下のプログラムで間違っている部分が分かる方、ご指摘お願い致します。 私の稚拙な腕では、どこが間違っているのかさっぱり分かりません。 以下のプログラムは、クラスticketを継承して、airplaneTicket、cinemaTicketを作成し、チケットの情報を入力し、最後に出力するものです。コンパイルエラーの箇所はコメントアウトしておきました。メイン関数の中に計5箇所あります。 よろしくお願い致します。 #include<string> #include<iostream> #include<vector> using namespace std; class ticket{ public: string ID; string seat; int price; ticket(){ cout << "ticket default constructor" << endl; ID="not Known"; seat="not Known"; } ticket(string i, string t){ cout << "ticket parameter-constructor" << endl; ID=i; seat=t; } virtual void setPrice(int p)=0; void printInfo(){ cout << ID << " " << seat << endl; } }; class airplaneTicket: public ticket{ public: string flight; airplaneTicket(){ cout << "airplane default constructor" << endl; flight="not known yet"; } airplaneTicket(string i, string t, string f):ticket(i, t){ cout << "airplane parameter-constructor" << endl; flight=f; } virtual void setPrice(int p){ string type; cout << "enter class type: "; cin >> type; if(type=="business") price=p*2; else price=p; } void priceInfo(){ cout << ID << " " << seat << " " << price << " " << flight << endl; } }; class cinemaTicket: public ticket{ public: cinemaTicket(){ cout << "cinema default constructor" << endl; } cinemaTicket(string i, string t):ticket(i, t){ cout << "cinema parameter-constructor" << endl; } }; int main(){ ticket tList[100]; //error: invalid abstract type 'ticket' for 'tList' tList[0]=new airplaneTicket("239", "d34", "f345"); //error: no match for 'operator=' in 'tList[0] tList[1]=new airplaneTicket(); // error: no match for 'operator=' in 'tList[1] tList[2]=new cinemaTicket("245", "a23"); //error: cannot allocate an object of abstract type 'cinemaTicket' tList[3]=new cinemaTicket(); // error: cannot allocate an object of abstract type 'cinemaTicket for(int i=0;i<4; i++){ tList[i].printInfo(); } return 1; }

  • コンパイルエラー: LNK2001

    今、ベクトル計算を簡単にするクラスを作ってみようとしています。 //vector3.h template<class TT> class vector3{ public:   enum{NUM=3};   TT x[NUM];   void set(const TT *vv)void set(const TT *vv){     int i;     for(i=0;i<NUM;i++){       x[i]=vv[i];     }   } }; のように、set関数をクラスの中に書いていると問題無いのですが、以下のように、 //vector3.h template<class TT> class vector3{ public:   enum{NUM=3};   TT x[NUM];   void set(const TT *vv); }; //test.cpp #include"vector3.h" template<class TT> void vector3<TT>::set(const TT *vv){   int i;   for(i=0;i<NUM;i++){     x[i]=vv[i];   } } と、cppファイルの中に書き換えると以下のようにエラーが出るようになります。 error LNK2001: 外部シンボル ""public: void __thiscall vector3<double>::set(double const *)" (?set@?$vector3@N@@QAEXPBN@Z)" は未解決です。 fatal error LNK1120: 外部参照 1 が未解決です。 これはいったい何故なのでしょうか? 使用しているのはVisualC++2008ExpressEditionです 宜しくお願いします。

  • 継承でのエラーについて

    現在C++を勉強しているのですが、教えてください。 サブクラス作成時のエラーなのです。 長いですがソース見てください。 勝手にインデントが無くなるので見にくいですが…。 #include <iostream> #include <string> using namespace std; class Hito{ private: string Name; public: Hito(string); //コンストラクタ void SelfIntro(); //自己紹介する }; //Hitoのコンストラクタ Hito::Hito(string str){ Name = str; } //自己紹介するメソッド void Hito::SelfIntro(){ cout << "私は、" << Name << " です。" << endl; } class Syusyo: public Hito{ private: int Ninki; public: Syusyo(string,int); void SayNinki(); }; //Syusyoのコンストラクタ Syusyo::Syusyo(string str, int year){ Hito(str); Ninki = year; } void Syusyo::SayNinki(){ cout << "私の任期は" << Ninki << "年です。" << endl; } int main(){ string str = "小泉アホの助"; int year = 5; Syusyo hito1(str,year); hito1.SelfIntro(); hito1.SayNinki(); return 0; } HitoクラスからSyusyoクラスを作っています。 そのSyusyoクラスのコンストラクタでエラーが出るのです。 内容は、 「デフォルトコンストラクタがありません。」 と言うものです。 参考書通りに Syusyo::Syusyo(string str, int year):Hito(str),Ninki(year){} とすればエラーにはなりません。 なぜ前出の文だとエラーになり、後出の文は大丈夫なのでしょうか? 文の表現が違うだけだと思うのですが…。

  • 【C言語・BLAS】 行列ベクトル積におけるエラー

    今、BLASを使って行列ベクトル積を計算するのにどれくらい時間がかかるか計測しようとしています。 しかし、短いプログラムにも関わらずC用インターフェースのスレッド並列版BLASを呼び出す部分でFloating point exceptionエラーが出てしまいます。 なぜエラーが出るのか全く分からないので、どなたか分かる方ご意見いただけないでしょうか? #define SEED 1 #define N 100 /* extern void dgemv(char transa, int m, int n, double alpha, double *a, int lda, double *x, int incx, double beta, double *y, int incy); */ int main( int argc, char *argv[] ){  char TRANS = 'T';  int INC = 1;  double ALPHA = 1.0;  double BETA = 0.0;  int i, j, n1, n2;  double **matrix;  double *vector;  double *result;  double start_time;  double end_time;  fprintf( stdout, "____performance evaluation start____\n" );  srand(SEED);  matrix = Malloc2DDouble( N, N );  vector = (double *) malloc ( sizeof(double) * N );  result = (double *) malloc ( sizeof(double) * N );  n1 = N; n2 = N;  #pragma omp parallel  {   #pragma omp for private(j)   for( i=0; i<n1; i++ ){    for( j=0; j<n2; j++ ){     matrix[i][j] = ( ( rand() / (double)RAND_MAX ) - 0.5 );    }    vector[i] = ( ( rand() / (double)RAND_MAX ) - 0.5 );    result[i] = 0.0;   }  }  start_time = GetTime();  dgemv( TRANS, n2, n1, ALPHA, matrix[0], n2, vector, INC, BETA, result, INC );  end_time = GetTime();  return EXIT_SUCCESS; }

  • セグメンテーションエラーがでます

    今、cでオセロゲームを作っています。 コンピュータと対戦できるようにしたいのですが、セグメンテーション違反になってしまいます。 p[LEN*LEN]を大域変数にするとうまく動きますが、以下のように局所変数にして関数で受け渡しをすると、 数回ループさせたところでエラーで強制終了します。 おそらく関数find_legal_moveへのp[]の渡し方が悪いのだと思いますが、なぜか分かりません。 以下にプログラムの一部を載せますので、お手数ですが原因を教えていただけないでしょうか。 よろしくお願いします。 #define LEN 10 /* ボードの1辺 */ #define opponent(player) (3-(player)) /* 1の相手は2, 2の相手は1 */ typedef struct {  int row; /* 行 */  int col; /* 列 */  int dr, dc; /* 行, 列の向き */ } Position; // 裏返る石の個数 int count_turn_over(int board[][LEN], int player, Position p) {  int i;  for (i=1; board[p.row+i*p.dr][p.col+i*p.dc]==opponent(player); i++);  if (board[p.row+i*p.dr][p.col+i*p.dc] == player)   return i - 1;  else   return 0; } // playerが(row, col)に石を置けるかどうかをチェック int is_legal_move(int board[][LEN], int player, Position p) {  if ((p.row < 1 || p.row > 8) || (p.col < 1 || p.col > 8))   return 0;  if (board[p.row][p.col] != 0) return 0;  for (p.dr = -1; p.dr <= 1; p.dr++)   for (p.dc = -1; p.dc <= 1; p.dc++)    if (count_turn_over(board, player, p) != 0)     return 1;  return 0; } // playerがどこに石を置けるか int find_legal_move(int board[][LEN], int player, Position p[]) {  Position pos;  int i = 0;  for (pos.row = 1; pos.row < LEN - 1; pos.row++)   for (pos.col = 1; pos.col < LEN - 1; pos.col++)    if (is_legal_move(board, player, pos) != 0) {     p[i].row = pos.row;     p[i].col = pos.col;     i++;    }  return i; } // computerの入力 void computer(int board[][LEN], int player, Position *pos) {  int i, num, max;  Position p[LEN * LEN];  printf("コンピュータの番です\n");  // 一番多く取れるところを取る  num = find_legal_move(board, player, p);  max = count_turn_over(board, player, p[0]);  pos->row = p[0].row;  pos->col = p[0].col;  for (i = 1; i < num; i++) {   int tmp = count_turn_over(board, player, p[i]);   if (max < tmp) {    pos->row = p[i].row;    pos->col = p[i].col;   }  } }