开发者

Linker Error in c++, It is just a simple class program, I am beginner at c++

开发者 https://www.devze.com 2023-02-06 02:02 出处:网络
#include<iostream> #include<stdlib.h> #include\"header_complex.h\" using namespace std; class 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::se开发者_如何学运维t_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;
}


error LNK2019: unresolved external symbol "public: __thiscall Complex::Complex(void)" (??0Complex@@QAE@XZ) referenced in function _main

Reading and understanding the error message is a very important skill to learn to get ahead. "Unresolved external" is an error message that the linker produces when it sees you using an identifier but cannot find a definition for it in any of the supplied .obj and .lib files.

Complex::Complex(void) is the missing identifier. It is the constructor of the Complex class that doesn't take any arguments. You declared it, you use it, you just didn't write the code for it.

You can get help on a linker or compiler error by selecting the error message in the Error List window and pressing F1.


I don't understand why anyone would describe an error message so vaguely (say, as a "linker error") without copying and pasting the error message. That said, I was able to compile and link your program on Linux by making the following changes:

1) I moved the class definition for Complex into a header file named header_complex.h, since that's what the main program #includes.

2) I added a definition for the default constructor:

Complex::Complex() : real(0), imag(0) {}

3) I added -lstdc++ to the command line:

gcc complex.cpp -lstdc++

By the way, I think you're going to want to modify your display method to add some endls:

  cout << "Real part is  " << real << endl;
  cout << "Imaginary part is  " << imag << endl;


You cannot provide default parameters at the function definition; you must do it at function declaration. Instead of declaring two constructors, declare one like

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

Then in your definition, remove the defaults, so it becomes

 Complex::Complex(float a, float b)


Emm... seems you're missing default constructor while being using it:

Complex c1; // here's the error - you have no defined Complex::Complex() {} method
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号