为什么我的C++程序无法编译("undefined reference")?
Why does my C++ program not compile ("undefined reference")?
我想写一个处理矩阵和数组的程序。因此,我编写了以下代码:
Array.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
Array.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;
}
Matrix.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
Matrix.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;
}
main.cpp
#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'
我的代码有什么问题,我应该如何正确编译我的文件(不使用任何makefiles)?谢谢!
将"main.cpp"添加到编译行:
$ c++ -Wall -pedantic -o matrix Array.cpp Matrix.cpp main.cpp
^^^^^^^^
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- 安卓 NDK 制造。数百"undefined reference error"
- 当我尝试进行TPC-E测试时,实用程序抱怨"undefined reference"
- 声明和定义函数静态会产生"undefined reference to function_name()"
- C++ : "undefined reference to"可能是因为CMake文件
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 生成错误:"cannot find target for file"和"undefined reference"
- AppData\Local\Temp\cc59LXDc.o:test1.cpp: undefined reference to 'constructNewObjectOfClass
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"