Undefined reference Error C++

Programming, for all ages and all languages.
Post Reply
Mehmetdev1
Posts: 23
Joined: Fri Apr 01, 2022 10:06 am
Location: Türkiye, Uşak/Merkez

Undefined reference Error C++

Post by Mehmetdev1 »

Hello, C++ is getting an undefined frequency error. I am attaching the C++ Source Codes and the Screenshot of the Error.

port.cpp soucres code:

Code: Select all

#include "port.h"

Port::~Port()
{
}



Port8Bit::Port8Bit(uint16_t portnumber)
: Port(portnumber)
{
}

Port8Bit::~Port8Bit()
{
}

void Port8Bit::Write(uint8_t data)
{
   __asm__ volatile("outb %0, %1" : : "a" (data), "Nd" (portnumber));
}

uint8_t Port8Bit::Read()
{
     uint8_t result;
    __asm__ volatile("inb %1, %0" : "=a" (result) : "Nd" (portnumber));
    return result;
}




Port8BitSlow::Port8BitSlow(uint16_t portnumber)
: Port8Bit(portnumber)
{
}

Port8BitSlow::~Port8BitSlow()
{
}

void Port8BitSlow::Write(uint8_t data)
{
   __asm__ volatile("outb %0, %1\njmp 1f\n1: jmp 1f\n1:" : : "a" (data), "Nd" (portnumber));
}





Port16Bit::Port16Bit(uint16_t portnumber)
: Port(portnumber)
{
}

Port16Bit::~Port16Bit()
{
}

void Port16Bit::Write(uint16_t data)
{
   __asm__ volatile("outw %0, %1" : : "a" (data), "Nd" (portnumber));
}

uint16_t Port16Bit::Read()
{
      uint16_t result;
    __asm__ volatile("inw %1, %0" : "=a" (result) : "Nd" (portnumber));
    return result;
}



Port32Bit::Port32Bit(uint16_t portnumber)
: Port(portnumber)
{
}

Port32Bit::~Port32Bit()
{
}

void Port32Bit::Write(uint32_t data)
{
   __asm__ volatile("outl %0, %1" : : "a" (data), "Nd" (portnumber));
}

uint32_t Port32Bit::Read()
{
      uint16_t result;
    __asm__ volatile("inl %1, %0" : "=a" (result) : "Nd" (portnumber));
    return result;
}

port.h soucres code:

Code: Select all

#ifndef __PORT_H
#define __PORT_H


#include "types.h"



    class Port
    {
    protected:
        uint16_t portnumber;
        Port(uint16_t portnumber);
	~Port();
    };
    
    
    class Port8Bit : public Port
    {
    public:
        Port8Bit(uint16_t portnumber);
	~Port8Bit();
	virtual void Write(uint8_t data);
	virtual uint8_t Read();
    };
    
    
     class Port8BitSlow : public Port8Bit
    {
    public:
        Port8BitSlow(uint16_t portnumber);
	~Port8BitSlow();
	virtual void Write(uint8_t data);
    };
    
    
    
      class Port16Bit : public Port
    {
    public:
        Port16Bit(uint16_t portnumber);
	~Port16Bit();
	virtual void Write(uint16_t data);
	virtual uint16_t Read();
    };
    
    
      class Port32Bit : public Port
    {
    public:
        Port32Bit(uint16_t portnumber);
	~Port32Bit();
	virtual void Write(uint32_t data);
	virtual uint32_t Read();
    };
    
    
#endif

Github Repos: https://github.com/mehmetprogramci/Kapilar_OS
Attachments
Port.cpp C++ Hatası.png
Last edited by Mehmetdev1 on Fri Apr 22, 2022 5:47 am, edited 2 times in total.
M. Alp
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Undefined reference Error C++

Post by Ethin »

Well, I can't tell you exactly what's causing the error, but your linker is telling you what's missing. So we don't need to tell you that. But your code is confusing and (for me) hard to follow. First, you don't need to redefine portnumber in every descendant class. Since port is the progenitor of all the different port class types, referring to it will automatically refer to the class member variable in the base class. Second, you don't need all those descendant classes. It would simplify your code dramatically if you used a template class and then just instantiated that for each port type (u8, u16, u32), or used function overloads within the port class itself. And you also don't need a destructor -- there's nothing to "destroy" so you can let the compiler handle that for you automatically via the default destructor. Finally, your implementation of port*slow::write is broken: you have two labels, 1, and I'm pretty sure that's not going to assemble because I'm pretty sure you can't define two labels with the same name. But maybe this is a querk of the GNU As assembler that I just don't know about and this is perfectly okay.
Would you mind giving us a GitHub repository link and posting the error instead of a screenshot?
Edit: actually, there aren't any screenshots attached anyway, so nobody is going to know what your linker is complaining about other than you.
User avatar
iansjack
Member
Member
Posts: 4688
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Undefined reference Error C++

Post by iansjack »

Numeric labels have a special usage in as. They have local scope and can be redefined.
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Undefined reference Error C++

Post by Ethin »

iansjack wrote:Numeric labels have a special usage in as. They have local scope and can be redefined.
Oh okay, I thought that would be an assemble-time error. Though at my first quick glance I thought it would be an infinite loop so I had to re-read it and then I revised that assessment.
User avatar
iansjack
Member
Member
Posts: 4688
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Undefined reference Error C++

Post by iansjack »

The “f” or “b” after the number in the jump instruction refer to the first label with that number searching forwards or backwards from the current IP. It can be quite useful, particularly in macros.
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Undefined reference Error C++

Post by davmac314 »

Ethin wrote:First, you don't need to redefine portnumber in every descendant class.
I'm looking at the code, and it doesn't do that. Only the Port class defines the portnumber field, in the subclasses portnumber only appears as a constructor parameter.
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Undefined reference Error C++

Post by Ethin »

davmac314 wrote:
Ethin wrote:First, you don't need to redefine portnumber in every descendant class.
I'm looking at the code, and it doesn't do that. Only the Port class defines the portnumber field, in the subclasses portnumber only appears as a constructor parameter.
Oh I thought I saw that... My bad. And my re-reads still yielded that... Huh.
User avatar
iansjack
Member
Member
Posts: 4688
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Undefined reference Error C++

Post by iansjack »

Show us the error message you are getting.
linuxyne
Member
Member
Posts: 211
Joined: Sat Jul 02, 2016 7:02 am

Re: Undefined reference Error C++

Post by linuxyne »

A required constructor for the Port class is missing.
nullplan
Member
Member
Posts: 1769
Joined: Wed Aug 30, 2017 8:24 am

Re: Undefined reference Error C++

Post by nullplan »

You are declaring that Port has a constructor. It is implicitly used by all subclasses. Therefore, you must define a constructor for class Port. And if it is empty.
Carpe diem!
Mehmetdev1
Posts: 23
Joined: Fri Apr 01, 2022 10:06 am
Location: Türkiye, Uşak/Merkez

Re: Undefined reference Error C++

Post by Mehmetdev1 »

Ethin wrote:Well, I can't tell you exactly what's causing the error, but your linker is telling you what's missing. So we don't need to tell you that. But your code is confusing and (for me) hard to follow. First, you don't need to redefine portnumber in every descendant class. Since port is the progenitor of all the different port class types, referring to it will automatically refer to the class member variable in the base class. Second, you don't need all those descendant classes. It would simplify your code dramatically if you used a template class and then just instantiated that for each port type (u8, u16, u32), or used function overloads within the port class itself. And you also don't need a destructor -- there's nothing to "destroy" so you can let the compiler handle that for you automatically via the default destructor. Finally, your implementation of port*slow::write is broken: you have two labels, 1, and I'm pretty sure that's not going to assemble because I'm pretty sure you can't define two labels with the same name. But maybe this is a querk of the GNU As assembler that I just don't know about and this is perfectly okay.
Would you mind giving us a GitHub repository link and posting the error instead of a screenshot?
Edit: actually, there aren't any screenshots attached anyway, so nobody is going to know what your linker is complaining about other than you.
Hello, The codes for the error are in the Port.cpp and port.h files. I've Created the Github Repository And Uploaded the Image Related to the Error What is the Error Exactly?

Github Repos: https://github.com/mehmetprogramci/Kapilar_OS
M. Alp
thewrongchristian
Member
Member
Posts: 425
Joined: Tue Apr 03, 2018 2:44 am

Re: Undefined reference Error C++

Post by thewrongchristian »

Mehmetdev1 wrote:
Ethin wrote:Well, I can't tell you exactly what's causing the error, but your linker is telling you what's missing. So we don't need to tell you that. But your code is confusing and (for me) hard to follow. First, you don't need to redefine portnumber in every descendant class. Since port is the progenitor of all the different port class types, referring to it will automatically refer to the class member variable in the base class. Second, you don't need all those descendant classes. It would simplify your code dramatically if you used a template class and then just instantiated that for each port type (u8, u16, u32), or used function overloads within the port class itself. And you also don't need a destructor -- there's nothing to "destroy" so you can let the compiler handle that for you automatically via the default destructor. Finally, your implementation of port*slow::write is broken: you have two labels, 1, and I'm pretty sure that's not going to assemble because I'm pretty sure you can't define two labels with the same name. But maybe this is a querk of the GNU As assembler that I just don't know about and this is perfectly okay.
Would you mind giving us a GitHub repository link and posting the error instead of a screenshot?
Edit: actually, there aren't any screenshots attached anyway, so nobody is going to know what your linker is complaining about other than you.
Hello, The codes for the error are in the Port.cpp and port.h files. I've Created the Github Repository And Uploaded the Image Related to the Error What is the Error Exactly?

Github Repos: https://github.com/mehmetprogramci/Kapilar_OS
Exactly what your error screen shot says:

Port::Port(unsigned short) is missing. You declare it, use it, but don't define an implementation of it.
Post Reply