Accessing classes in the same C++ project - c++

In my C++ project, one of the .cpp files has a class declared. Now I want to instantiate this class in another .cpp file in the same project, but I get this error message:
error C2248: 'Processor' : cannot access private member declared in class 'Processor'
c:\Test\wrapper.cpp : see declaration of 'Processor'
We can't redefine the class using a different access specifier, it gets the default private access specifier.
How can I make this work?

You can't (in a clean way at least), and you shouldn't.
Making fields private (even by default) is the programmer's way of telling you you're not supposed to instantiate this class.
Also, if a class is defined inside a cpp file, it's only visible in that translation unit. You don't include cpp files. If you must, move the definition inside a header and include that, but not the cpp.

Do not include the cpp file directly in your cpp file, it might cause all sort of problems. The class is inside a cpp file means that you are not supposed to use it from any other cpp file, if you really need that class then you need to move that class to an appropriate header and include the header file. But before doing that, first check whether any alternate ways to do what you are trying to do without exposing the new class.

Related

Is it good C++ to define classes in .cpp file?

I am a beginner in C++. I have a question regarding C++ design.
My file contains A,B,C,D,E class definitions. Class A contains the API which is used by other applications. I have defined this in a .h file. Classes B,C,D & E define concrete classes by inheriting an abstract class which is provided by some library. These definitions are not used by any external application, but only used by class A.
I have seen online that all the class definitions are provided in an .h file and the function implementations in a .cpp file. My question here is, even though class B,C,D & E definitions are not used externally by anyone, should I define them in the .h file? If I do define them there anyway, I cannot expose them to other applications, right?
If a class is only used locally in one module, you can declare it in the .cpp file. This is actually good practice; don't expose more than necessary.
In a case where you need to define a class (or function, etc.) in a header (for example, to share it between several related .cpp file) but you do not want to expose it publicly, you can have a separate, private header file which is only included in the relevant places, but is not made public. This can be hinted at by appending "private" to the header file name.

C++ helper functions in class, how to use them?

In my class in C++ I want to write a helper function (Which external users can't see or use) and found 2 ways to do that:
1) To declare it as private in the .h file and write the implementation in .cpp file.
2) To write the implementation directly in .cpp file without declaring it in the .h file.
What's the correct way, the difference or the advantage one have on the other?
Since you are not exposing this function through your interface, you should not declare it in the public header file. If you use it in a single cpp file, declare it in an anonymous namespace in that cpp file. If you use this function in multiple cpp files but you would still like to not make it a part of your interface, you can create internal header files that you #include in your source files, but aren't a part of your library's public interface.
The way is to create a class A that would be visible to the public, then create a descendant class B of this class and implement it privately. Then, when you are asked to create an A*, create a B* which is also an A* and return it. Your code sees a B* but the user of the class sees A* and may only access methods and variables declared in A. You also need to create/destroy your pointers with some function rather than with new/delete.
Pretty much like how COM implements IUnknown and stuff.

enum type needed on multiple class header files

I am trying to make a C++ program that uses multiple classes (and multiple header files). I have made an enum type called 'move' in one of the classes but I need to access this type from all of the classes. If I leave the enum declaration in just one header file, the other classes can't access it, the compiler gives an error everywhere its used except in that class and main. If I declare it in main, no class can access it. If I declare it in every class header file, I get a compiler error for redefining it.
Where do I declare an enum type so that every class header file has access to it?
Where do I declare an enum type so that every class header file has access to it?
In its own header. Make a separate header file for your enum, and #include that header in all other headers that need to use it. Don't forget to add include guards to avoid multiple inclusions:
#ifndef MOVE_H
#define MOVE_H
enum move foo {
LEFT, RIGHT, UP, DOWN
};
#endif /* MOVE_H */
Note: If you have multiple enumerations that logically belong together, or an enum that belongs together with a class, you may want to put the two in the same header.

How to make a template function in test.h use a static method inside test.cpp

Currently the template functions are inside the .cpp file, however these templates need to be defined in the header. However, when I move them in to the header, the templates have no access to the static methods inside the .cpp file. What changes do I need for this to work?
This is just a utility header and cpp, nothing to do with classes.
I would just go ahead and put declarations of all needed functions in a header file (and remove the static from their definitions). If you put them in namespace FunctionName_impl_detail or something like that, you're safe from name collisions. If you put comments near the declarations discouraging using them directly, you probably don't need to "hide" or "protect" them. But if you want to mostly prevent other code from using them, you can make them private, static members of some class which friends the template functions.
You should define the static method in the header.
Template classes have to be defined entirely in the header file because the compiler basically creates a new class for each template instantiation, so it has to have access to the full method implementation at compilation time.

Two classes that refer to each other

I'm new to C++, so this question may be basic:
I have two classes that need to refer to each other. Each is in its own header file, and #include's the other's header file. When I try to compile I get the error "ISO C++ forbids declaration of ‘Foo’ with no type" for one of the classes. If I switch things so the opposite header gets parsed first I get the same error with the other class.
Is it possible in C++ to have two classes that need references to each other?
For more detail: I have an "App" class and a "Window" class. App needs to refer to Window to make the window. Window has a button that calls back to App, so it needs a reference to App. If I can't have two classes refer to each other, is there a better way to implement this?
You can use forward declarations in the header files to get around the circular dependencies as long as you don't have implementation dependencies in the headers. In Window.h, add this line:
class App;
In App.h, add this line:
class Window;
Add these lines before the class definitions.
Then in the source files, you include the headers for the actual class definitions.
If your class definitions reference members of the other class (for example, in inlines), then they need to be moved to the source file (no longer inline).
Forward declaration is the way to go.
If you are using pointers\reference in class header then Forward declaration at both sides would work for you.
If you are creating the object as a class member then you must include header itself. ( Forward declaration won't work as compiler needs class definition for knowing the size).
Refer C++ FAQ for solving such senario:
If you are creating the Window as member then include the Window header in App but at the same time Window shouldn't include the App's header. Use the combination of pointer to App and the forward declaration there.