Including header file causes error, while including cpp file does not

Was playing around with code from a class, making the input into a class, the class works properly when it's inside the smae file as main, but when I place them into their .h and .cpp files and then include the header file and not the cpp file it returns these errors. including the cpp file causes no errors though
1
2
3
4
5
ccwL4uzw.o	main.cpp:(.text+0xab): undefined reference to `stock::resetShares()'
G:\Users\WILLIA~1\AppData\Local\Temp\ccwL4uzw.o	main.cpp:(.text+0x12c): undefined reference to `stock::addShares(int)'
G:\Users\WILLIA~1\AppData\Local\Temp\ccwL4uzw.o	main.cpp:(.text+0x148): undefined reference to `stock::setPrice(int, int, int)'
G:\Users\WILLIA~1\AppData\Local\Temp\ccwL4uzw.o	main.cpp:(.text+0x154): undefined reference to `stock::getShareValue()'
G:\Users\William Porter\Desktop\Stocks Class Test\collect2.exe	[Error] ld returned 1 exit status



My main.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/***************************************************************
    Existing classes, objects, and functions used in this program. 
    Explain what items are used from each of these header files.
***************************************************************/
#include <iostream>  // what is used from here? Gives access to cin, cout
#include "stocks.h"
#include <limits>
using namespace std; // eliminates needing the "std::" prefix

/***************************************************************
    struct definitions 
***************************************************************/

//none



/***************************************************************
    global variables (rare)
***************************************************************/

//none

/***************************************************************
    New function prototypes. Include a description of the function
    input(s) and the function output. Often some data is helpful:
             
    int largest( int, int, int );   // largest(4,5,-1) returns 5
***************************************************************/

//This function takes in the whole dollar value of as stocck and the numerator and
//denominator of it's fractional value and converts it into a double value.
double calculateStockPrice(int dollars, int top, int bottom); // calculateStockPrice(2.0, 1.0, 2.0) returns 2.5


/***************************************************************
    main() function code.
    The main() function always takes zero parameters/arguments
    and returns 0 to indicate that the input was successfully
    processed into output. 
    
    Later: A return of [some other value] means [something else].
****************************************************************/

int main()
{
	int shares = 0;
    int wholeNumber = 0;
    int numerator = 0;
    int denominator = 0;
    double stockPrice = 0.0;
    double stockValue = 0.0;
    
    cout.setf(ios::fixed); //displays fixed numbers decimals
    cout.precision(2); //displays two numbers after the decimal for fixed numbers
    
    stock test;
    
    //welcome message
    cout << "Welcome to the stock value calculator\n";
    //ask for number of shares
    cout << "\nEnter the number of shares in a stock[0 or less to exit]:\n";
    //get number of shares
    cin >> shares;
    
    //if they have no shares get out
    while (shares > 0.0)
    {
    	test.resetShares();
    	//ask for whole number of the price
    	cout << "Enter the whole dollar value of the stock:\n";
    	//get whole number of price
    	cin >> wholeNumber;
    	//ask for numerator of price fraction
    	cout << "Enter the top part of the fractional value(numerator):\n";
    	//get numerator of price fraction
    	cin >> numerator;
    	//ask for denominator of price fraction
    	cout<< "Enter the bottom part of the fractional value:\n";
    	//get denominator of price fraction
    	cin >> denominator;
    	
//	   	cout << test.getNumOfShares();
		test.addShares(shares);
		test.setPrice(wholeNumber, numerator, denominator);
		
		cout << test.getShareValue() << "\n";

	    //ask for number of shares from user again
    	cout << "\nEnter the number of shares in a stock[0 or less to exit]:\n";
	    //get number of shares
	    cin >> shares;
	}
	
	cout << "\nGood-Bye\n"; //say good bye
	
	cin.sync();
	cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
//    system("pause");    // why is this here? What does it do? Pauses the screen so the user can see whats displayed.
    return 0;           // return 0 to indicate successful execution of main()
}

/***************************************************************
    Code for other functions
    Use the main() comment above as the model.
***************************************************************/

//This function takes in the whole dollar value of as stocck and the numerator and
//denominator of it's fractional value and converts it into a double value.
double calculateStockPrice(int dollars, int top, int bottom)
{
	double calculation = 0.0; // new var for calculations
	
	// calculate stock price in doubles,  stock price = whole dollar value + (top/bottom)
	calculation = (static_cast<double>(dollars) + ( static_cast<double>(top) / static_cast<double>(bottom) ) );
	
	return calculation; // send calculation back to function call as the answer
}

stocks.h
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
#ifndef STOCKS_H
#define STOCKS_H

class stock
{
public:
	stockName(char names[]);
	
	addShares(int share);
	
	removeShares(int share);
	
	resetShares();
	
	int	getNumOfShares();
	
	setPrice(int wholeNum, int top, int bottom);
	
	setPrice(double lp_price);
	
	int getStockPrice();
	
	double getShareValue();
	
private:
	char s_name[5];
	int s_shares;
	double s_price;
};

#endif 

my stocks.cpp
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
#include <iostream>
#include "stocks.h"
#include <stdlib.h>
using namespace std; // eliminates needing the "std::" prefix



stock::stockName(char names[])
{
	s_name[0] = names[0];
	s_name[1] = names[1];
	s_name[2] = names[2];
	s_name[3] = names[3];
	s_name[4] = names[4];	
}

stock::addShares(int share)
{
	s_shares = s_shares + share;
}

stock::removeShares(int share)
{
	s_shares = s_shares - share;
}

int	stock::getNumOfShares()
{
	return s_shares;
	exit(0);
}

stock::setPrice(int wholeNum, int top, int bottom)
{
	s_price = ( static_cast<double>(wholeNum) + ( static_cast<double>(top) / static_cast<double>(bottom)));
}

stock::setPrice(double lp_price)
{
	s_price = lp_price;
}

int stock::getStockPrice()
{
	return s_price;
}

double stock::getShareValue()
{
	double value = 0.0;
	double numShares = 0.0;
	numShares = static_cast<double>(s_shares);
	value = (numShares*s_price);
	return value;
}
stock::resetShares()
{
	s_shares = 0;
}


I've got no idea why it doesn't work properly, I'm compiling using dev c++ orwell 5.11.
closed account (LA48b7Xj)
It builds ok for me, is the .cpp file included as part of the project?

I just noticed that you don't have return values on some of the member functions, maybe that is the reason.
Last edited on
all my files are in the same folder, this bugs me quite a bit.

The ones that don't have return values on them are meant to set the values inside the class. Should I be tossing return values in them?
What compiler are you using? I get numerous errors trying to compile your program with a standards compliant compiler.

stocks.h
--------
Line 7,9,11,13,17,19: No return type specified. If these functions don't return anything, they should be type void

stocks.cpp
----------
Line 8: Why not use std::string for stock name?

Line 30: The exit statement will never be reached. Why is it there?

main.cpp
--------
Line 67: Why are you comparing shares (int) to a double?




using g++, best error report i've seen so far, what compiler are you using?

Going to work on changing those mentioned lines and then repost the updated code.
Okay here's the updated code

still getting the same errors when compiling as the first post.

stocks.h (changed to void now, still doesn't working when included, while the .cpp file does.)
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
#ifndef STOCKS_H
#define STOCKS_H

class stock
{
public:
	void stockName(char names[]);
	
	void addShares(int share);
	
	void removeShares(int share);
	
	void resetShares();
	
	int	getNumOfShares();
	
	void setPrice(int wholeNum, int top, int bottom);
	
	void setPrice(double lp_price);
	
	int getStockPrice();
	
	double getShareValue();
	
private:
	char s_name[5];
	int s_shares;
	double s_price;
};

#endif 


stocks.cpp (what do you mean std::string for the name? I know i can but it's another lib to add and not really affecting the running (yet), i'll probably change it in the future, exit statement was leftovers from a test on why it wasn't working.)
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
#include <iostream>
#include "stocks.h"
#include <stdlib.h>
using namespace std; // eliminates needing the "std::" prefix



void stock::stockName(char names[])
{
	s_name[0] = names[0];
	s_name[1] = names[1];
	s_name[2] = names[2];
	s_name[3] = names[3];
	s_name[4] = names[4];	
}

void stock::addShares(int share)
{
	s_shares = s_shares + share;
}

void stock::removeShares(int share)
{
	s_shares = s_shares - share;
}

int	stock::getNumOfShares()
{
	return s_shares;
}

void stock::setPrice(int wholeNum, int top, int bottom)
{
	s_price = ( static_cast<double>(wholeNum) + ( static_cast<double>(top) / static_cast<double>(bottom)));
}

void stock::setPrice(double lp_price)
{
	s_price = lp_price;
}

int stock::getStockPrice()
{
	return s_price;
}

double stock::getShareValue()
{
	double value = 0.0;
	double numShares = 0.0;
	numShares = static_cast<double>(s_shares);
	value = (numShares*s_price);
	return value;
}
void stock::resetShares()
{
	s_shares = 0;
}


main.cpp (the error was due to having shares be a bouble at one point)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/***************************************************************
    Existing classes, objects, and functions used in this program. 
    Explain what items are used from each of these header files.
***************************************************************/
#include <iostream>  // what is used from here? Gives access to cin, cout
#include "stocks.h"
#include <limits>
using namespace std; // eliminates needing the "std::" prefix

/***************************************************************
    struct definitions 
***************************************************************/

//none



/***************************************************************
    global variables (rare)
***************************************************************/

//none

/***************************************************************
    New function prototypes. Include a description of the function
    input(s) and the function output. Often some data is helpful:
             
    int largest( int, int, int );   // largest(4,5,-1) returns 5
***************************************************************/

//This function takes in the whole dollar value of as stocck and the numerator and
//denominator of it's fractional value and converts it into a double value.
double calculateStockPrice(int dollars, int top, int bottom); // calculateStockPrice(2.0, 1.0, 2.0) returns 2.5


/***************************************************************
    main() function code.
    The main() function always takes zero parameters/arguments
    and returns 0 to indicate that the input was successfully
    processed into output. 
    
    Later: A return of [some other value] means [something else].
****************************************************************/

int main()
{
	int shares = 0;
    int wholeNumber = 0;
    int numerator = 0;
    int denominator = 0;
    double stockPrice = 0.0;
    double stockValue = 0.0;
    
    cout.setf(ios::fixed); //displays fixed numbers decimals
    cout.precision(2); //displays two numbers after the decimal for fixed numbers
    
    stock test;
    
    //welcome message
    cout << "Welcome to the stock value calculator\n";
    //ask for number of shares
    cout << "\nEnter the number of shares in a stock[0 or less to exit]:\n";
    //get number of shares
    cin >> shares;
    
    //if they have no shares get out
    while (shares > 0)
    {
    	test.resetShares();
    	//ask for whole number of the price
    	cout << "Enter the whole dollar value of the stock:\n";
    	//get whole number of price
    	cin >> wholeNumber;
    	//ask for numerator of price fraction
    	cout << "Enter the top part of the fractional value(numerator):\n";
    	//get numerator of price fraction
    	cin >> numerator;
    	//ask for denominator of price fraction
    	cout<< "Enter the bottom part of the fractional value:\n";
    	//get denominator of price fraction
    	cin >> denominator;
    	
//	   	cout << test.getNumOfShares();
		test.addShares(shares);
		test.setPrice(wholeNumber, numerator, denominator);
		
		cout << test.getShareValue() << "\n";

	    //ask for number of shares from user again
    	cout << "\nEnter the number of shares in a stock[0 or less to exit]:\n";
	    //get number of shares
	    cin >> shares;
	}
	
	cout << "\nGood-Bye\n"; //say good bye
	
	cin.sync();
	cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
//    system("pause");    // why is this here? What does it do? Pauses the screen so the user can see whats displayed.
    return 0;           // return 0 to indicate successful execution of main()
}

/***************************************************************
    Code for other functions
    Use the main() comment above as the model.
***************************************************************/

//This function takes in the whole dollar value of as stocck and the numerator and
//denominator of it's fractional value and converts it into a double value.
double calculateStockPrice(int dollars, int top, int bottom)
{
	double calculation = 0.0; // new var for calculations
	
	// calculate stock price in doubles,  stock price = whole dollar value + (top/bottom)
	calculation = (static_cast<double>(dollars) + ( static_cast<double>(top) / static_cast<double>(bottom) ) );
	
	return calculation; // send calculation back to function call as the answer
}
Last edited on
stocks.cpp
Line 44: You're trying to return a double, but the function is declared to return an int.

Other than that, I get no errors compiling and executing your program.
Are you getting the same errors as in your original post?
If not, please post the current errors.

Based on the original errors, it would seem the linker can not find the functions in your stocks.cpp file. Are you sure that stocks.cpp is part of your project?

The fact that the problem goes way when you #include "stocks.cpp" (which you should never do), lends credence to stocks.cpp not being part of your project.

Last edited on
How do I add files to a project? I'm still getting those same errors as the first post with this new one when i use a project file.
1
2
3
4
5
6
G:\Users\William Porter\Desktop\Stocks Class Test\main.o	main.cpp:(.text+0xab): undefined reference to `stock::resetShares()'
G:\Users\William Porter\Desktop\Stocks Class Test\main.o	main.cpp:(.text+0x12c): undefined reference to `stock::addShares(int)'
G:\Users\William Porter\Desktop\Stocks Class Test\main.o	main.cpp:(.text+0x148): undefined reference to `stock::setPrice(int, int, int)'
G:\Users\William Porter\Desktop\Stocks Class Test\main.o	main.cpp:(.text+0x154): undefined reference to `stock::getShareValue()'
G:\Users\William Porter\Desktop\Stocks Class Test\collect2.exe	[Error] ld returned 1 exit status
25		Makefile.win	recipe for target 'Stocks.exe' failed


Here's the produced makeFile

MakeFile.win
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
# Project: Stocks
# Makefile created by Dev-C++ 5.11

CPP      = g++.exe
CC       = gcc.exe
WINDRES  = windres.exe
OBJ      = main.o
LINKOBJ  = main.o
LIBS     = -L"G:/Program Files/Dev-Cpp/MinGW64/lib" -L"G:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc
INCS     = -I"G:/Program Files/Dev-Cpp/MinGW64/include" -I"G:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"G:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS  = -I"G:/Program Files/Dev-Cpp/MinGW64/include" -I"G:/Program Files/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"G:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"G:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN      = Stocks.exe
CXXFLAGS = $(CXXINCS) 
CFLAGS   = $(INCS) 
RM       = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
	${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
	$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

main.o: main.cpp
	$(CPP) -c main.cpp -o main.o $(CXXFLAGS)
Last edited on
I see no make rule for
 
stocks.o:  stocks.cpp


Nor do I see stocks.o included in your link.

How to add a file to your project depends on what IDE you're using (assuming you're using an IDE).




using orwell dev c++ 5.11, when I make a project in it it gives me a space to program main into.
Aha I figured out how to add files to my project, thanks so much.
1
2
3
4
//socks.h
//write this file to make others understood better
// your code
.....


1
2
3
4
//socks.cpp
//your code
# include "socks.h"
.....


1
2
3
4
//main.cpp
//your code
# include "socks.cpp"
.......
@blacker - No. One should NEVER #include .cpp files. They should be separately compiled.

Also your second snippet is also wrong. The #include should be BEFORE the user code.

Last edited on
Topic archived. No new replies allowed.