C++ c+中的链接器错误+;,这只是一个简单的课堂程序,我是c+的初学者+; #包括 #包括 #包括“header_complex.h” 使用名称空间std; 阶级情结 { 私人: 浮点实数,imag; 公众: Complex();//默认的cnstructor 复数(float,float);//参数化构造函数 无效集_实(浮动); 无效集_imag(浮动); float get_real(); float get_imag(); 无效输入(); void display(); }; int main() { 复杂c1;//对象创建 c1.显示(); c1.输入(); c1.显示(); 返回退出成功; } //函数定义 复杂::复杂(浮点a=0,浮点b=0) { real=a; imag=b; } void Complex::set_real(浮点a) { a=真实; } void Complex::set_imag(浮点a) { a=imag; } float Complex::get_real() { 回归真实; } float Complex::get_imag() { 返回图像; } void Complex::input(){ 真实的; cout>imag; } void Complex::display() { cout

C++ c+中的链接器错误+;,这只是一个简单的课堂程序,我是c+的初学者+; #包括 #包括 #包括“header_complex.h” 使用名称空间std; 阶级情结 { 私人: 浮点实数,imag; 公众: Complex();//默认的cnstructor 复数(float,float);//参数化构造函数 无效集_实(浮动); 无效集_imag(浮动); float get_real(); float get_imag(); 无效输入(); void display(); }; int main() { 复杂c1;//对象创建 c1.显示(); c1.输入(); c1.显示(); 返回退出成功; } //函数定义 复杂::复杂(浮点a=0,浮点b=0) { real=a; imag=b; } void Complex::set_real(浮点a) { a=真实; } void Complex::set_imag(浮点a) { a=imag; } float Complex::get_real() { 回归真实; } float Complex::get_imag() { 返回图像; } void Complex::input(){ 真实的; cout>imag; } void Complex::display() { cout,c++,C++,不能在函数定义中提供默认参数;必须在函数声明中提供默认参数。与其声明两个构造函数,不如声明一个类似的构造函数 #include<iostream> #include<stdlib.h> #include"header_complex.h" using namespace std; class Complex { private: float real,imag; public: Complex(); //default cnstructor Complex

不能在函数定义中提供默认参数;必须在函数声明中提供默认参数。与其声明两个构造函数,不如声明一个类似的构造函数

#include<iostream>
#include<stdlib.h>
#include"header_complex.h"

using namespace std;




class Complex
{
private:
 float real,imag;
public:
 Complex(); //default cnstructor
 Complex(float, float); //parametrize constructor
 void set_real(float);
 void set_imag(float);
 float get_real();
 float get_imag();
 void input();
 void display();

};


int main()
{
 Complex c1;  // object creation

 c1.display();
 c1.input();
 c1.display();

 return EXIT_SUCCESS;
}


//Functions definations

Complex::Complex(float a=0, float b=0)
{
  real = a;
  imag = b;
}

 void Complex::set_real(float a)
{
  a = real;
}

void Complex::set_imag(float a)
{
  a = imag;
}

float Complex::get_real()
{
  return real;
}
float Complex::get_imag()
{
  return imag;
}

void Complex::input(){
  cout << "Enter Real part ";
  cin >> real;
  cout << "Enter Imaginary part " ;
  cin >> imag;
}

void Complex::display()
{
  cout << "Real part is  " << real;
  cout << "Imaginary part is  " << imag;
}
然后在您的定义中,删除默认值,使其成为

 Complex(float a=0, float b=0);

我不明白为什么有人会在不复制和粘贴错误消息的情况下如此含糊地描述错误消息(例如,作为“链接器错误”)。也就是说,我能够通过以下更改在Linux上编译和链接您的程序:

1) 我将Complex的类定义移动到名为
header\u Complex.h
的头文件中,因为这是主程序
包含的内容

2) 我为默认构造函数添加了一个定义:

 Complex::Complex(float a, float b)
3) 我在命令行中添加了
-lstdc++

Complex::Complex() : real(0), imag(0) {}
顺便说一句,我想您需要修改显示方法,添加一些
endl
s:

gcc complex.cpp -lstdc++

coutEmm…似乎您在使用默认构造函数时丢失了它:

  cout << "Real part is  " << real << endl;
  cout << "Imaginary part is  " << imag << endl;
错误LNK2019:未解析的外部 符号“公共:\此呼叫” 复杂::复杂(无效)” (??0复杂@@QAE@XZ)引用于 主功能

阅读和理解错误消息是学习成功的一项非常重要的技能。“Unresolved external”是链接器在看到您使用标识符但在提供的任何.obj和.lib文件中找不到该标识符的定义时生成的错误消息

Complex::Complex(void)是缺少的标识符。它是不接受任何参数的复杂类的构造函数。您声明了它,使用了它,只是没有为它编写代码


通过在错误列表窗口中选择错误消息并按F1键,您可以获得有关链接器或编译器错误的帮助。

请格式化代码,谢谢。请使用{}格式化源代码好的,但问题是什么?有错误吗?什么不起作用-不要害怕包含详细信息。链接器错误意味着您在链接代码时可能没有提供一些库。如何编译和链接此程序?您缺少默认构造函数的实现,您这样做的方式是含糊不清的。您是对的,但是,在我添加了默认构造函数定义之后,它在linux上用g++为我编译,甚至在抛出了
-pedantic
标志的情况下也是如此。奇怪。这并不完全正确。在函数定义中指定默认参数是完全合法的,只要在声明中没有指定它们。您可以查看更多信息我在这个概念中混淆了。我是否可以合并两个构造函数?考虑你的默认构造函数是什么样子的。它会把代码>真的< /代码>和<代码> IMAG < /代码>到某个默认值(例如0)吗?如果是的话,你就可以使用默认的参数到<代码>复合体(浮点,浮点)。,这样当你有类似代码<复杂的C1;< /C> >编译器会自动将它转换成<代码>复杂的C1(0,0);也考虑如果有人尝试<代码>复杂C1(3)会发生什么;< /COD>编译器会将此转换为<代码>复杂C1(3 0)如果这是您想要的,那么您应该使用默认值。这可以消除混淆吗?但是我合并两个构造函数是合法的,有些人这样说。您可以合并它们,但是如果合并,您不应该在类定义中声明
Complex();
。只声明
Complex(float,float);
并为参数提供默认值。
Complex c1; // here's the error - you have no defined Complex::Complex() {} method