1

我正在尝试编写一个处理矩阵和数组的程序。因此,我编写了以下代码:

数组.h

#ifndef _ARRAY_H_
#define _ARRAY_H_

class Array{
    private:
        int * data;
        int length;

public:
    Array();
    Array(int size);
    ~Array();

    void set(int pos, int value);
    int get(int pos);
    void print();

    /*works only for arrays of length 9*/
    static int find_max(int data[]);
};
#endif

数组.cpp

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Array.h"

using namespace std;

Array::Array(){
    Array(10);
}

Array::Array(int size){
    data = new int[size];
    memset(data, 0, sizeof(data));
    length = size;
}

Array::~Array(){
    delete [] data;
}

...

/*works only for arrays of length 9*/
int Array::find_max(int data[]){
    int max = data[0];

    for(int i = 1; i < 9; i++){
        if(data[i] > max) max = data[i];
    }

    return max;
}

矩阵.h

#ifndef _MATRIX_H_
#define _MATRIX_H_
#include "Array.h"

class Matrix{

    private:
        Array * data;
        int height;
        int width;

    public:
        Matrix();
        Matrix(int _height, int _width);
        ~Matrix();

        void set(int h, int w, int value);
        int get(int h, int w);
        void print();
};

#endif

矩阵.cpp

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Matrix.h"

using namespace std;

Matrix::Matrix(){
    Matrix(10, 10);
}

Matrix::Matrix(int _height, int _width){
    height = _height;
    width = _width;

    data = (Array*)malloc(sizeof(Array)*height);

    for(int i = 0; i < height; i++){
        Array * row = new Array(width);
        *(data + i) = *row;
    }
}

 ...

void Matrix::print(){
    for(int i = 0; i < height; i++){
        Array row = *(data + i);
        row.print();
    }
    return;
}

主文件

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "Array.h"
#include "Matrix.h"

using namespace std;

int main(int argc, char** argv){
    if(argc != 3){
        cout << "usage: " << argv[0] << " <m> x <n>" << endl;
    }

    int m = atoi(argv[1]);
    int n = atoi(argv[2]);

    Matrix myMatrix(m, n);

    /*fill matrix randomly*/
    int guess, minus;
    srand(time(NULL));

    for(int r = 0; r < m; r++){
        for(int c = 0; c < n; c++){
            guess = rand() % 1001;
            minus = rand() % 2;

            if(minus == 0) guess *= -1;
            myMatrix.set(r, c, guess);;

        }
    }

    cout << "randomly created matrix" << endl;
    myMatrix.print();

    /*find local maximum and print it in another matrix*/
    Matrix localMaxMatrix(m, n);

    for(int r = 0; r < m; r++){
        for(int c = 0; c < n; c++){
            /*correct access is ensured within get method*/
            int values[] = {myMatrix.get(r-1, c-1),
                            myMatrix.get(r-1, c),
                            myMatrix.get(r-1, c+1),
                            myMatrix.get(r,   c-1),
                            myMatrix.get(r,   c),
                            myMatrix.get(r,   c+1),
                            myMatrix.get(r+1, c-1),
                            myMatrix.get(r+1, c),
                            myMatrix.get(r+1, c+1)};
            localMaxMatrix.set(r, c, Array::find_max(values));
        }
    }

    cout << "----------------------------------------" << endl;
    cout << "local max for each entry of above matrix" << endl;
    localMaxMatrix.print();

    return 0;
}

编译它会c++ -Wall -pedantic -o matrix Array.cpp Matrix.cpp导致以下编译错误:

/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../libcygwin.a(libcmain.o): In function 'main': /usr/src/debug/cygwin-1.7.17-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to '_WinMain@16'

我的代码有什么问题,我应该如何正确编译我的文件(不使用任何 makefile)?谢谢!

4

1 回答 1

4

将“main.cpp”添加到您的编译行:

$ c++ -Wall -pedantic -o matrix Array.cpp Matrix.cpp main.cpp
                                                     ^^^^^^^^
于 2013-05-05T19:14:53.850 回答