new class header

Unfortunately, I made a class that has over 1000 code lines.
Is it possible to add another file to split class.cpp ?
Yes, having a class split into multiple source files is possible as long as you compile and link the multiple files.
Does your class need 1000 lines of code? If so then don't worry about the size of the file. Looking at the source directory where I'm currently working, there are 82 cpp files. 15 of them have more than 1000 lines. The largest is 9,742 lines. The class needs them all and there's no compelling reason to split up the file, so it stays as one file.
Yeah honestly 1000 lines isn't too bad.

If you start to have over 9,000 lines in a file, chances are there might be something you could refactor into different logical parts, but sometimes it's just clearer to have a big file.

I will say though, if you are ever working with other people. it's sometimes harder to make changes to a file if it's a really popular file that everyone else is also making changes to. That's my (personal) #1 reason for splitting up files into finer logical parts.

But see also: https://wiki.c2.com/?GodClass
Last edited on
If I add files to my class, for splitting member functions in shortest file, I receive error because class doesn't find implementation of methods that I bring to another file..
Are you including your class header file in each .cpp implementation file? Do you even have a header file?
I receive error because

Please, show the error. We can't see (from here) whether your compiler or your linker is baffled.
CMakeFiles\Algoritmi2.dir/objects.a(Circuit.cpp.obj): In function `std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::_M_erase(__gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >)':
D:/Programmi/MinGW/mingw64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/bits/vector.tcc:157: multiple definition of `_dictionary[abi:cxx11]'
CMakeFiles\Algoritmi2.dir/objects.a(main.cpp.obj):main.cpp:(.bss+0x0): first defined here
CMakeFiles\Algoritmi2.dir/objects.a(Circuit.cpp.obj):Circuit.cpp:(.bss+0xc0): multiple definition of `_gate[abi:cxx11]'
CMakeFiles\Algoritmi2.dir/objects.a(main.cpp.obj):main.cpp:(.bss+0xc0): first defined here
CMakeFiles\Algoritmi2.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/user/CLionProjects/Algoritmi2/main.cpp:8: undefined reference to `Circuit::printConiLogici()'
C:/Users/user/CLionProjects/Algoritmi2/main.cpp:9: undefined reference to `Circuit::findMin()'
CMakeFiles\Algoritmi2.dir/objects.a(Circuit.cpp.obj): In function `Circuit::Circuit()':
C:/Users/user/CLionProjects/Algoritmi2/Circuit.cpp:16: undefined reference to `Circuit::checkFeedback()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\Algoritmi2.dir\build.make:115: Algoritmi2.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/Algoritmi2.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/Algoritmi2.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: Algoritmi2] Error 2
This is what happen if I try to split class.cpp
Are you including your class header file, and other required system headers, in each .cpp implementation file? From the errors it doesn't look like you are.
There seem to be two different errors:

* The "undefined reference" is a linker error; no translation unit (given to linker) provides implementations for those members. You forgot to compile&link something.

* The "multiple definition" is also a linker error; multiple translation units provide implementation for same thing. ODR violation.

Foo.h
1
2
3
4
5
6
7
#ifndef FOO_H
#define FOO_H
struct Foo {
  Foo();
  int xxx() const;
};
#endif 

Foo.cpp
1
2
3
#include "Foo.h"

Foo::Foo() {}

Fooxx.cpp
1
2
3
4
5
#include "Foo.h"

int Foo::xxx() const {
  return 42;
}

Bar.cpp
1
2
3
4
5
6
#include "Foo.h"

int main() {
  Foo ff;
  int x = ff.xxx();
}

Save those four files.
Then define CMake project that includes the Foo.cpp, Fooxx.cpp, and Bar.cpp
Build.

If you get errors, then your CMake-fu is weak.
Topic archived. No new replies allowed.