Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的C++;程序未编译(“未定义引用”)?_C++_Compiler Errors - Fatal编程技术网

C++ 为什么我的C++;程序未编译(“未定义引用”)?

C++ 为什么我的C++;程序未编译(“未定义引用”)?,c++,compiler-errors,C++,Compiler Errors,我想写一个处理矩阵和数组的程序。因此,我编写了以下代码: 数组.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);

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

数组.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
#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 "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;
}
#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;
}
矩阵.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;
}
#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;
}
#包括
#包括
#包括
#包括“矩阵h”
使用名称空间std;
矩阵::矩阵(){
矩阵(10,10);
}
矩阵::矩阵(整数高度,整数宽度){
高度=_高度;
宽度=_宽度;
数据=(数组*)malloc(数组大小)*高度);
对于(int i=0;i
main.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;
}
#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;
}
#包括
#包括
#包括
#包括“Array.h”
#包括“矩阵h”
使用名称空间std;
int main(int argc,字符**argv){
如果(argc!=3){
无法将“main.cpp”添加到编译行:

$ c++ -Wall -pedantic -o matrix Array.cpp Matrix.cpp main.cpp
                                                     ^^^^^^^^
将“main.cpp”添加到编译行:

$ c++ -Wall -pedantic -o matrix Array.cpp Matrix.cpp main.cpp
                                                     ^^^^^^^^

我想您忘了将main.cpp与Array.cpp和Matrix.cpp一起添加。while编译字符串应该类似于
c++-Wall-pedantic-o Matrix Array.cpp Matrix.cpp main.cpp
阅读并遵守:以未定义引用开头的链接器错误表明错误消息中的符号不在可执行文件中:it either未定义,或已定义,但包含符号的翻译单元未添加到链接器命令行。这不是问题,而是名称以下划线开头,后跟大写字母(
\u ARRAY\u H
)并且包含两个连续下划线的名称保留给实现。不要使用它们。我认为您忘记了将main.cpp与Array.cpp和Matrix.cpp一起添加。while编译字符串应该类似于
c++-Wall-pedantic-o Matrix Array.cpp Matrix.cpp main.cpp
阅读并遵守以下内容:开始的链接器错误如果引用未定义,则表示错误消息中的符号不在可执行文件中:该符号未定义,或者已定义,但包含该符号的翻译单元未添加到链接器命令行。这不是问题所在,而是名称以下划线开头,后跟大写字母(
\u ARRAY\u H
)包含两个连续下划线的名称保留给实现。不要使用它们。我怎么可能没有看到?!谢谢!它现在可以编译了,但我发现程序中止了,在调试器中我得到消息“glibc检测到…程序接收信号SIGABRT,中止了。0xb7fdd424 in_uuukernel_vsyscall()”但我不知道这是否与我编译程序的方式有关…@StringerBell:错误消息可能告诉你glibc检测到了什么,这是错误中的重要部分。也就是说,它检测到了一些好的东西,但知道它检测到了什么对发现错误很重要[很可能它检测到双重自由?]您的程序中有一些错误,但您应该提出单独的问题:例如,默认的
Array
构造函数没有执行您可能认为的操作(如果这是您所期望的,它不会将调用转发给1 arg构造函数)@DavidRodríguez dribeas好的,谢谢,我会再看一遍,如果有必要,我会在单独的问题中处理这些问题。我怎么可能没有看到这个?!谢谢!它现在确实编译了,但我发现程序中止了,在调试器中我得到了消息“检测到glibc…程序接收到信号SIGABRT,已中止。内核vsyscall()中的0xb7fdd424”“但我不知道这是否与我编译程序的方式有关…@StringerBell:错误消息可能告诉你glibc检测到了什么,这是错误中的重要部分。也就是说,它检测到了一些好的东西,但知道它检测到了什么对于发现错误很重要[很可能它检测到双重自由?]您的程序中有一些错误,但您应该提出单独的问题:例如,默认的
Array
构造函数没有执行您可能认为的操作(如果这是您所期望的,它不会将调用转发给1 arg构造函数)@DavidRodríguez dribeas好的,谢谢,我会再看一次,如果有必要,我会在单独的问题中处理这些问题。