0

I want to call function from class but it show me error. my test.cxx file is

#include<iostream>
#include"Scalar.hxx"
using namespace std;
int main()
{
    Scalar Text;
    char a;
    a=Text.SetBank(1);
    cout<<("%x",a);
    return 0;
    }

it show me error

undefined reference to `Scalar::Scalar()'
undefined reference to `Scalar::SetBank(char)'

Scalar.hxx

#ifndef __SCALAR_HXX__
#define __SCALAR_HXX__
#include <stdint.h>
class Scalar
{
    public:
        Scalar(void);
        char SetBank(char bank_no);
};
#endif

i have included correct file name.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Some questions: Are the files in the same folder? Do you get any warnings? Show full compiler output if you have not. What is your compilation line? – kabanus Feb 13 '20 at 06:54
  • You have to compile the source file that defines the `Scalar` class (`Scalar.cxx`?) and link that object file with your program. Including the header allows your code to compile to object file — it doesn't ensure that the object code for the class is available when your program is linked. – Jonathan Leffler Feb 13 '20 at 06:54
  • Can you show us the commands you are using for this? Both for compiler and linker, and any make file if there is one? – Tanveer Badar Feb 13 '20 at 06:59
  • no i just run it using g++ text.cxx command,i also don't have any warnings. – unknowndeveloper Feb 13 '20 at 07:04

2 Answers2

0

I do not think this is a compiler error in itself. Compiler is finding the definition just fine, but your linker cannot find the object files which contain the compiled code for Scalar and hence complains.

I have also commented requesting more data to help you with resolving this.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
0

Scalar.hxx provides declaration for Scalar and SetBank methods but I see no definition. Is there Scalar.cxx file containing the code like the following?

Scalar::Scalar() {
    // ctor's body
}

char Scalar::SetBank(char bank_no) {
    // method's body
}

If there is then does it get compiled (i.e., included in your project/makefile)?

PooSH
  • 601
  • 4
  • 11