Undefined reference to Function in header file

I have two .cpp files one is main1.cpp and second main2.cpp. I have function on main2.cpp which I am calling on main1.cpp via header file named main2.h.

I am getting error and it says "Undefined reference to Printlines"

This one is main1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "main2.h"
using namespace std;

int main()
{
 char CharToDisplay;
 int NumberOfChar, RowToDisplay;
 // Prompt the user to enter a value
 cout << "Enter any positive decimal integer value: ";
 // To get char into ch
 cin >> CharToDisplay >> NumberOfChar >> RowToDisplay;
 Printlines (CharToDisplay, NumberOfChar, RowToDisplay);
 return 0;
}




This one is main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "main2.h"
using namespace std;

void Printlines(int CharToDisplay, int NumberOfChar, int RowToDisplay)
{
 // the loop that controls the Row
 for (int row = 0; row < RowToDisplay; row++)
 {
 // the loop that controls the Column
    for (int column = 0; column < NumberOfChar; column++)
    {
        cout << (char)CharToDisplay;
    }
 cout << "\n";
 }
}


This is header file main2.h
1
2
3
4
5
6
7
#ifndef MAIN2
#define MAIN2

void Printlines(int CharToDisplay, int NumberOfChar, int RowToDisplay);

#endif // MAIN2
How do you compile/build your project?
Are you sure that you have main2.cpp included in your project?
Hey I am using Code::Blocks software!
You probably haven’t given the proper compilaton instruction to your IDE. Your code compiles and runs in W10 + MinGW by the following intruction:
g++ -std=c++2a -Werror -Wall -Wextra -Wpedantic -Wshadow -O2 main1.cpp main2.cpp -o main.exe

Output:
Enter any positive decimal integer value: G 5 10
GGGGG
GGGGG
GGGGG
GGGGG
GGGGG
GGGGG
GGGGG
GGGGG
GGGGG
GGGGG

Maybe you need to add main2.cpp to the project. Project->Add files
Topic archived. No new replies allowed.