Write a post

C++ FAQ

Published Oct 17, 2016Last updated Apr 12, 2017
C++ FAQ

1. How do I call a method from another class?

Static methods can be called directly (i.e., without having to instantiate the class) but these don’t allow us to change any data that isn’t static—so you probably don't want to do this. There are several ways to call a method other than static, described here is the most common way. You need to have an object (instance) of that class and call the method through that object. Next is the code in C++:

A.h

class B;
class A
{
public:
    A(B* b);
    void Func();
    void Print();
 
private:
    B* m_b;
};

A.cpp

#include <iostream>
#include "A.h"
#include "B.h"
 
A(B* b)
{
    m_b = b;
}
 
void A::Func()
{
    m_b->Func();
}
 
void A::Print()
{
  std::cout << "A";
}

B.h

class A;
class B
{
public:
    B(A* a);
    void Func();
    void Print();
 
private:
    A* m_a;
};

B.cpp

#include <iostream>
#include "B.h"
#include "A.h"
 
B(A* a)
{
    m_a = a;
}
 
void B::Func()
{
    m_a->Func();
}
 
void B::Print()
{
  std::cout << "B";
}

2. How do I solve linking errors?

Linking/linkage errors happen when you are not linking your project with a library that contains the definition of a function you’re using from that library. They may also happen when you have a declaration of a function but you don’t implement it and then you use it somewhere. It will give a link error with your function’s name. If you’re using the Visual Studio IDE, you may choose which libraries you want to link by going to the:

Project properties > Linker > Input > Additional dependencies

Then just add the correct, respective libraries. If you’re using some functions from the Windows API, you’ll probably need one or more of the following libraries: shell32.lib, kernel32.lib, user32.lib.

3. My header files depend on one another, how do I solve this circular dependency?

You can solve this by either changing the logic of your program and removing the dependency from one class in the other (having only one class depending instead of both), or you can use forward declarations to solve this circular dependency.

A.h

class B;
 
class A
{
private:
    B* m_b;
};

B.h

class A;
 
class B
{
private:
    A* m_a;
};

Since A depends on B and B on A, if we try to include B.h in A.h and vice versa, we’ll get a compiler error. To overcome this you have to use forward declarations shown in the example and make sure that the class isn’t being used (this includes holding an instance of a class instead of a pointer or reference to an object of the class). The forward declaration tells the compiler that there’s a type named A (or B depending of the file) but doesn’t give the compiler any information about that type and that’s the reason why you can’t call any methods or use a forward declared class. In the respective source files (cpp files) you may include both A.h and B.h and use the classes as you wish. Note that there’s no problem when you include both header files in the .cpp files because there’s no circular dependency.

4. What’s wrong with the C++ hello world?

#include <iostream>
 
int main()
{
    cout << "Hello World!";
    return 0;
}

I have crossed paths with many people who learned C++ from books that can’t compile the hello world. If you’re one of those that have problems compiling this code then you should better double check if the book or tutorial you're learning from is worth your time. The code doesn’t compile because cout isn’t accessible this way. All types/objects/functions of the C++ standard library are in a namespace called std. Namespaces allow you to group types/objects/functions under a name. So to correct your hello world, you could do one of the following:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!";
    return 0;
}
#include <iostream>
using std::cout;
int main()
{
    cout << "Hello World!";
    return 0;
}
#include <iostream>
 
int main()
{
    std::cout << "Hello World!";
    return 0;
}

5. Why do I need header files?

Well, you don’t.

But for large projects, it’s advisable that you use header files. Usually, header files contain declarations that later may be used in different files by including this header file. By separating declarations from the actual implementations you get some advantages:

  • The interface (declarations in the header file) is separated from the implementation (definitions in the source files).
  • Better structured files for better reading of interfaces.
  • It helps with reusing and/or maintaining the code.

6. Why does my application run in debug mode but not in release?

Debug and release modes don’t actually “exist”, they are just compiler settings which cause the application to behave differently. In debug mode, the compiler sets/clears/initializes the variables so it can track memory allocations/writings and this doesn’t happen (unless you changed the configurations) when you compile in release mode. These uninitialized variables may cause a crash in your program. This is not the only cause of the crashes in release mode but it's one that's very common.

Note: Also, be careful with code that is only ran in one or other configuration settings either by using macros (#ifdef #endif) and/or asserts.

One remote possibility is the compiler is causing the crash when optimizing your code. Optimizations are usually turned on in release configurations to speed up the application. And due to these changes, the compiler may introduce a bug in your code.

7. How can I debug my release build?

Yes, it is possible to debug release builds. Under Visual Studio go to:

Project Properties > C/C++ > Debug Information Format > Program Database
Project Properties > Linker > Debugging > Generate Debug Info > Yes

Now you can debug your program. But also be aware that if you have optimizations turned on, then you may not see the instruction pointer (yellow arrow) in the right place because the optimizations make the code change or even disappear (whenever it’s not needed) from the final build.

Did we miss anything? Feel free to add other C++ FAQ and answers in the comment section below.


Discover and read more posts from Ricardo
get started
Enjoy this post?

Leave a like and comment for Ricardo

Create a Cross-platform App with Visual Studio
7 Tips to Improve WordPress Security
Beginner Mistakes to Avoid in UI Design