[UPDATE] a reproducible example
By splitting declaration and definition into different files, my toy code built without problem...
my main.cpp
#include <iostream>
#include "file0.h"
#include "file1.h"
#include "file2.h"
int main() {
test a;
double b = a.get_recipocal(2.0);
double c = a.get_sqrt(2.0);
std::cout << "b = " << b << std::endl;
std::cout << "c = " << c << std::endl;
return 0;
}
my file0.h
#ifndef TEST_MULTI_FILES_FILE0_H
#define TEST_MULTI_FILES_FILE0_H
class test {
private:
double a;
public:
double get_recipocal(int a);
double get_sqrt(int a);
};
#endif//TEST_MULTI_FILES_FILE0_H
my file1.h
#ifndef TEST_MULTI_FILES_FILE1_H
#define TEST_MULTI_FILES_FILE1_H
double test::get_recipocal(int a) {
return 1. / a;
}
#endif//TEST_MULTI_FILES_FILE1_H
my file2.h
#include <cmath>
#ifndef TEST_MULTI_FILES_FILE2_H
#define TEST_MULTI_FILES_FILE2_H
double test::get_sqrt(int a) {
return sqrt(a);
}
#endif//TEST_MULTI_FILES_FILE2_H
Built and run without problem. So it is not an obvious error in my production code
Thanks!
(below are original question) I am new to C++ classes, here is my problem
for example, I have a class in file b0.h
class B::virtual public A {
void func1() {
some work1;
}
void func2() {
some work2;
}
...
};
Now I want to move the implementation of func2()
into an other header file, say b1.h
so I did the following:
#include "path/to/b0.h"
void B::func2(){
some work2;
}
But the compiler will complain
use of undeclared identifier 'B'
void B::func2() {
^
(I also tested the following: in b0.h
, I included b1.h
and if I made func2()
in b1.h
a static function, I can call it from b0.h
without problem.)
The goal is to separate members of B
into two header files, each one contain one function implementation (I want to have a side-by-side comparison between func1 and func2 in different file, they are similar and very long). I feel this is a very common scenario, but I didn't find a good example. Is there an obvious mistake? Thank you!