Home > Software design >  Error when including struct in a header file C "cannot overload functions distinguished by re
Error when including struct in a header file C "cannot overload functions distinguished by re

Time:08-18

I have the following structures that work fine when included in a single main.cpp file. But, when I try to move things to a header.h I get an error.

An example, main.cpp with full definitions (before moved to header.h file):

using namespace std;

    static const int nx = 10;
    static const int ny = 10; 
   
struct cplx_buffer
{
  fftw_complex* a;
  int rows;
  int cols;

  fftw_complex& operator()(int i, int j) const { return a[i * cols   j]; }
};
cplx_buffer my_fftw_allocate_cplx(int x, int y) { return cplx_buffer{ fftw_alloc_complex(x * y), x, y }; }
void print_matrix(cplx_buffer const& m)
{
  std::cout << std::fixed << std::setprecision(2);

  for (int i = 0; i < m.rows; i  )
  {
    for (int j = 0; j < m.cols; j  )
    {
      std::cout << std::setw(16) << std::complex<double> { m(i, j)[0], m(i, j)[1] };
    }
    std::cout << '\n';
  }
  std::cout << '\n';
}
int main(){

cplx_buffer outY = my_fftw_allocate_cplx((ny 1), nx);

   //stuff
  

}

Then my attempt of dividing things to header.h, header.cpp, and main.cpp files as the following:

header.h

using namespace std;
static const int nx = 10;
static const int ny = 10; 

struct cplx_buffer
{
  fftw_complex* a;
  int rows;
  int cols;

  fftw_complex& operator()(int i, int j) const { return a[i * cols   j]; }
};
void print_matrix(cplx_buffer const& m);

cplx_buffer my_fftw_allocate_cplx(int x, int y);

header.cpp looks like:

#include "header.h"
using namespace std;


struct cplx_buffer
{
  fftw_complex* a;
  int rows;
  int cols;

  fftw_complex& operator()(int i, int j) const { return a[i * cols   j]; }
};
cplx_buffer my_fftw_allocate_cplx(int x, int y) { return cplx_buffer{ fftw_alloc_complex(x * y), x, y }; } //ERROR

void print_matrix(cplx_buffer const& m)
{
  std::cout << std::fixed << std::setprecision(2);
  for (int i = 0; i < m.rows; i  )
  {
    for (int j = 0; j < m.cols; j  )
    {
      std::cout << std::setw(16) << std::complex<double> { m(i, j)[0], m(i, j)[1] };
    }
    std::cout << '\n';
  }
  std::cout << '\n';
}

The Main.cpp is:

#include "header.h" 
using namespace std;

int main(){
cplx_buffer outY = my_fftw_allocate_cplx((ny 1), nx);

   //stuff
  
}


The error comes from the header.cpp file in line:

cplx_buffer my_fftw_allocate_cplx(int x, int y) { return cplx_buffer{ fftw_alloc_complex(x * y), x, y }; } //ERROR

The full error message:

cannot overload functions distinguished by return type alone

CodePudding user response:

As someone in the comments suggested, the solution was to only define my struct in the header.h file and not to redefine it again in the header.cpp file.

  • Related