Compile two cpp files with QT Creator

Hi, folks!
Need to compile:

#ifndef TEST_H
#define TEST_H



int a = 0;
void simplePrint ();

#endif // TEST_H

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <Test.h>

using namespace std;
using std::ios;

void simplePrint ()
{
cout << "a " << a << endl;
}


#include <iostream>
#include <Test.h>
using namespace std;
using std::ios;

int main()
{
cout << "Hello World!" << endl;
return 0;
}


PRO_FILE
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
Test.cpp \
main.cpp

HEADERS += \
Test.h


:-1: error: debug/main.o:C:\Users\iceberg\Desktop\Test4\build-Test4-Desktop_Qt_6_2_3_MinGW_64_bit-Debug/../../TestBsae/Test4/Test.h:6: multiple definition of `a'; debug/Test.o:C:\Users\iceberg\Desktop\Test4\build-Test4-Desktop_Qt_6_2_3_MinGW_64_bit-Debug/../../TestBsae/Test4/Test.h:6: first defined here
Last edited on
14 posts and still not using code tags.
Learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
You are defining int a = 0; in two different compilation units.

Put the declaration of an extern variable in the header, and define it in exactly one .cpp file.

1
2
3
4
5
6
7
// foo.h:
extern int a;   // a declaration
extern void foo();

// foo.cpp:
int a;   // a definition
void foo() { ++a; }

(from https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)

[Note: The 'extern' is not needed on the function declaration]
Last edited on
FYI, including user created header files don't use #include <header.h> syntax, use #include "header.h" .

As Ganado points out defining a variable in a header file puts the definition into your two source files. Either use his suggestion of extern with the actual definition in one source file, or declare the variable as static.

Linkage issues can be a real beast at times.

With a bit of refactoring and light rewriting to use the function here's the entire code bits showing how to use static:
Test.h
1
2
3
4
5
6
7
8
#ifndef TEST_H
#define TEST_H

static int a = 0;

void simplePrint();

#endif // TEST_H 

Test.cpp
1
2
3
4
5
6
7
8
#include <iostream>

#include "Test.h"

void simplePrint()
{
   std::cout << "a = " << a << '\n';
}

main.cpp
1
2
3
4
5
6
7
8
#include <iostream>

#include "Test.h"

int main()
{
   simplePrint();
}
a = 0

Using code tags can make a difference when reading and commenting on code, really.
Thanks! This one solved!

Have another issue:
Here is:
BaseClass
array of ints
DerivedClass
main function

Array of ints could be reached and called from main using object, but there is no data which was set in set function at BaseClass
How can I share Base Class Contents to other classes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef COMMISSION_H
#define COMMISSION_H



#include <string> // C++ standard string class
class CommissionEmployee
{
public:
CommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0 );
void setFirstName( const std::string & ); 
std::string getFirstName() const; 
void setLastName( const std::string & ); 
std::string getLastName() const; 
void setSocialSecurityNumber( const std::string & );
std::string getSocialSecurityNumber() const; 
void setGrossSales( double ); 
double getGrossSales() const; 
void setCommissionRate( double ); 
double getCommissionRate() const; 
double earnings() const; 
void print() const; 
int arrayOfInts[1000][13];
protected:
std::string firstName;
std::string lastName;
std::string socialSecurityNumber;
double grossSales; 
double commissionRate; 
}; 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#endif 

// Fig. 11.14: CommissionEmployee.cpp
// Class CommissionEmployee member-function definitions.
#include <iostream>
#include <stdexcept>
#include "CommissionEmployee.h" 
using namespace std;
// constructor
CommissionEmployee::CommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate )
: firstName( first ), lastName( last ), socialSecurityNumber( ssn )
{
setGrossSales( sales ); 
setCommissionRate( rate );
} // end CommissionEmployee constructor
// set first name
void CommissionEmployee::setFirstName( const string &first )
{
firstName = first; // should validate
} // end function setFirstName
// return first name
string CommissionEmployee::getFirstName() const
{
return firstName;
} // end function getFirstName
// set last name
void CommissionEmployee::setLastName( const string &last )
{
lastName = last; // should validate
} // end function setLastName
// return last name
string CommissionEmployee::getLastName() const
{
return lastName;
} // end function getLastName
// set social security number
void CommissionEmployee::setSocialSecurityNumber( const string &ssn )
{
socialSecurityNumber = ssn; // should validate
} // end function setSocialSecurityNumber
// return social security number
string CommissionEmployee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
} // end function getSocialSecurityNumber
// set gross sales amount
void CommissionEmployee::setGrossSales( double sales )
{
if ( sales >= 0.0 )
grossSales = sales;
else
throw invalid_argument( "Gross sales must be >= 0.0" );
} // end function setGrossSales
// return gross sales amount
double CommissionEmployee::getGrossSales() const
{
return grossSales;
} // end function getGrossSales
// set commission rate
void CommissionEmployee::setCommissionRate( double rate )
{
if ( rate > 0.0 && rate < 1.0 )
commissionRate = rate;
else
throw invalid_argument( "Commission rate must be > 0.0 and < 1.0" );
} // end function setCommissionRate
// return commission rate
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
} // end function getCommissionRate
// calculate earnings
double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
} // end function earnings
// print CommissionEmployee object
void CommissionEmployee::print() const
{
cout << "commission employee: "
<< getFirstName() << ' ' << getLastName()
<< "\nsocial security number: " << getSocialSecurityNumber()
<< "\ngross sales: " << getGrossSales()
<< "\ncommission rate: " << getCommissionRate();
} // end function print 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string> // C++ standard string class
#include "CommissionEmployee.h" // CommissionEmployee class declaration
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0, double = 0.0 );
void setBaseSalary( double ); // set base salary
double getBaseSalary() const; // return base salary
double earnings() const; // calculate earnings
void print() const; // print BasePlusCommissionEmployee object
private:
double baseSalary; // base salary
}; // end class BasePlusCommissionEmployee
#endif
[code]
#include <iostream>
#include <stdexcept>
#include "BasePlusCommissionEmployee.h"
using namespace std;
// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
// explicitly call base-class constructor
: CommissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary ); // validate and store base salary
} // end BasePlusCommissionEmployee constructor
// set base salary
void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw invalid_argument( "Salary must be >= 0.0" );
} // end function setBaseSalary
// return base salary
double BasePlusCommissionEmployee::getBaseSalary() const
{
    return baseSalary;
    } // end function getBaseSalary
    // calculate earnings
    double BasePlusCommissionEmployee::earnings() const
    {
    return getBaseSalary() + CommissionEmployee::earnings();
    } // end function earnings
    // print BasePlusCommissionEmployee object
    void BasePlusCommissionEmployee::print() const
    {
    cout << "base-salaried ";
    // invoke CommissionEmployee's print function
    CommissionEmployee::print();
    cout << "\nbase salary: " << getBaseSalary();
    } // end function print




1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <BasePlusCommissionEmployee.h>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
    BasePlusCommissionEmployee
    employee( "Bob", "Lewis", "333-33-3333", 5000, .04, 300 );

    cout << employee.int arrayOfInts[1000][13];

}
Last edited on
Your indentation is IMO less than desirable, plus a bit more vertical whitespace could be beneficial.

Did you notice I split up the various headers/source files into their own blocks of code using separate opening/closing code tags?

It might help to make it look less of a monolithic, intimidating dense wall of text for these decrepit and aged eyes to read and understand.

Just suggestin'.... :)
oh sorry! Fixed
I'm talking about employee.int arrayOfInts[1000][13]
Once again:
I've set some data to array of ints inside base class
I want to use data on derived classes to the base class
now its zeroes
must be what i've actually set
What exactly are you trying to do with line 7 in your main function? It looks like you are trying to create a 2D array of ints with 1000 rows and 13 columns and somehow shoving that resulting array into your employee class.
Please don't presume we know what headers you include. Your code snippet should have those includes so your code is complete and hopefully compileable.

http://www.sscce.org/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef COMMISSION_H
#define COMMISSION_H

#include <string>
class CommissionEmployee
{
public:
CommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0 );
void setFirstName( const std::string & );
std::string getFirstName() const;
void setLastName( const std::string & );
std::string getLastName() const;
void setSocialSecurityNumber( const std::string & );
std::string getSocialSecurityNumber() const;
void setGrossSales( double );
double getGrossSales() const;
void setCommissionRate( double );
double getCommissionRate() const;
double earnings() const;
void print() const;
int arrayOfInts[1000][13];
void setDate ();
protected:
std::string firstName;
std::string lastName;
std::string socialSecurityNumber;
double grossSales;
double commissionRate;
};
#endif





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string>
#include "CommissionEmployee.h"
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0, double = 0.0 );
void setBaseSalary( double );
double getBaseSalary() const;
double earnings() const;

void print() const;
private:
double baseSalary;
}; //
#endif






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#ifndef VACATION_H
#define VACATION_H
#include <string>
#include "CommissionEmployee.h"
class VacationEmployee : public CommissionEmployee
{
public:
VacationEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0, double = 0.0 );
void setBaseSalary( double );
double getBaseSalary() const;
double earnings() const;

void print() const;
private:
double baseSalary;
}; //
#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <stdexcept>
#include "CommissionEmployee.h"
using namespace std;

CommissionEmployee::CommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate )
: firstName( first ), lastName( last ), socialSecurityNumber( ssn )
{
setGrossSales( sales );
setCommissionRate( rate );
}

void CommissionEmployee::setFirstName( const string &first )
{
firstName = first;
}

string CommissionEmployee::getFirstName() const
{
return firstName;
}

void CommissionEmployee::setLastName( const string &last )
{
lastName = last;
}

string CommissionEmployee::getLastName() const
{
return lastName;
}

void CommissionEmployee::setSocialSecurityNumber( const string &ssn )
{
socialSecurityNumber = ssn;
}

string CommissionEmployee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
}
void CommissionEmployee::setGrossSales( double sales )
{
if ( sales >= 0.0 )
grossSales = sales;
else
throw invalid_argument( "Gross sales must be >= 0.0" );
}
double CommissionEmployee::getGrossSales() const
{
return grossSales;
}
void CommissionEmployee::setCommissionRate( double rate )
{
if ( rate > 0.0 && rate < 1.0 )
commissionRate = rate;
else
throw invalid_argument( "Commission rate must be > 0.0 and < 1.0" );
}
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
}
double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
}
void CommissionEmployee::print() const
{
cout << "commission employee: "
<< getFirstName() << ' ' << getLastName()
<< "\nsocial security number: " << getSocialSecurityNumber()
<< "\ngross sales: " << getGrossSales()
<< "\ncommission rate: " << getCommissionRate();
}


     void CommissionEmployee:: setDate  ()
     {
     arrayOfInts[0][0] = 30;
     arrayOfInts[0][1] = 5;
     arrayOfInts[0][2] = 1987;


     }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <stdexcept>
#include "BasePlusCommissionEmployee.h"
using namespace std;

BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )

: CommissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary );
}
void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw invalid_argument( "Salary must be >= 0.0" );
}

double BasePlusCommissionEmployee::getBaseSalary() const
{
    return baseSalary;
    }

    double BasePlusCommissionEmployee::earnings() const
    {
    return getBaseSalary();
    }

    void BasePlusCommissionEmployee::print() const
    {
    cout << "base-salaried ";

    BasePlusCommissionEmployee::print();
    cout << "\nbase salary: " << getBaseSalary();
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdexcept>
#include "VacationEmployee.h"
using namespace std;

VacationEmployee::VacationEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )

: CommissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary );
}
void VacationEmployee::setBaseSalary( double salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw invalid_argument( "Salary must be >= 0.0" );
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>
#include <iomanip>
#include "BasePlusCommissionEmployee.h"
#include "VacationEmployee.h"
using namespace std;
int main()
{

CommissionEmployee group1        ( "John ", "Doe", "Remote", 1000, 0.175 );
BasePlusCommissionEmployee group2( "Scott", "Fisher", "Office", 2000, 0.55 );
VacationEmployee group3          ( "David", "Owen", "Warehouse", 3000, 0.15 );


group1.setDate();
cout << group1.arrayOfInts[0][0] << endl;
cout << group2.arrayOfInts[0][0] << endl;
cout << group3.arrayOfInts[0][0] << endl;


}



Output:

30
0
0
Last edited on
So far your group2 and groupe3 objects are showing their [0][0]th elements are zero.

The sole difference group1 is calling the setDate() method on the object. That changes the [0][0]th element to 30.

Isn't that what you are looking for?

Expecting someone not you to have intimate knowledge of what your code is supposed to do merely by reading it, especially in a short period of time, ain't gonna happen. I have enough problems looking at code I wrote days, weeks and months before and remembering just what the hell it was supposed to do. :)

I need to use data set with Base class with derived classes..
I need to use data set with Base class with derived classes..

Great, what output did you expect?
Last edited on
Topic archived. No new replies allowed.