Stock Exchange Program Help

Cannot figure out the errors withing my program. Can anyone offer any advice.

Here are the errors:
midtermec.cpp:40: error: ‘stock’ has not been declared
midtermec.cpp:46: error: variable or field ‘addstock’ declared void
midtermec.cpp:46: error: ‘stock’ was not declared in this scope
midtermec.cpp: In member function ‘double Xchange::getPrice(std::string)’:
midtermec.cpp:53: error: expected unqualified-id before ‘=’ token
midtermec.cpp:53: error: ‘i’ was not declared in this scope
midtermec.cpp: In function ‘int main()’:
midtermec.cpp:66: error: ‘Xchange nsadaq’ redeclared as different kind of symbol
midtermec.cpp:65: error: previous declaration of ‘Xchange nsadaq()’
midtermec.cpp:67: error: request for member ‘addstock’ in ‘nsadaq’, which is of non-class type ‘Xchange()’
midtermec.cpp:67: error: ‘stock’ was not declared in this scope
midtermec.cpp:68: error: request for member ‘getPrice’ in ‘nsadaq’, which is of non-class type ‘Xchange()’

Here is the code
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
  /* Program: MidtermEC.cpp */

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Stock
{
public:
	Stock(string s, double p);
	string getSymbol();
	double getPrice();
	void changePrice( double newPrice);
private:
string symbol;
double price;
};

Stock::Stock(string s, double p)
{
	symbol= s;
	price= p;
}

string Stock::getSymbol()
{
	return symbol;
}

void Stock::changePrice (double newPrice)
{
	price = newPrice;
}

class Xchange: public Stock
{
public:
	Xchange();
	void addStock (stock newStock);
	double getPrice (string symbol);
private:
	vector <Stock> stocks;
};

void Xchange::addstock (stock newStock)
{
	stocks.push_back(newStock);
}

double Xchange::getPrice (string symbol)
{
	for(int=0; i<stocks.size; i++)
	{
	if (stocks[i].getSymbol== symbol)
		{
		return stocks[i].getPrice();
		}
	}
	return -1;
}

int main()
{
	Xchange nsadaq();
	Xchange nsadaq = Xchange();
	nsadaq.addstock (stock("GOOG", 1000));
	cout << nsadaq.getPrice ("GOOG");
}






Everywhere it says "'stock' was not declared..."
Capitalization, capitalization, capitalize. :)

Line 46: More capitalization issues.

Line 53: You're missing an i.

Line 65: FYI, you don't really need those parentheses.

Line 66: Look at the previous line and re-evaluate if you need this.

-Albatross
Been working on this for a bit now.

New errors:

midtermec.cpp: In member function ‘double Xchange::getPrice(std::string)’:
midtermec.cpp:53: error: invalid use of member (did you forget the ‘&’ ?)
midtermec.cpp:55: error: no match for ‘operator==’ in ‘((Xchange*)this)->Xchange::stocks.std::vector<_Tp, _Alloc>::operator[] [with _Tp = Stock, _Alloc = std::allocator<Stock>](((long unsigned int)i))->Stock::getSymbol == symbol’
midtermec.cpp: In function ‘int main()’:
midtermec.cpp:65: error: conversion from ‘Xchange*’ to non-scalar type ‘Xchange’ requested


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
/* Program: MidtermEC.cpp */

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Stock
{
public:
	Stock(string s, double p);
	string getSymbol();
	double getPrice();
	void changePrice( double newPrice);
private:
	string symbol;
	double price;
};

Stock::Stock(string s, double p)
{
	symbol= s;
	price= p;
}

string Stock::getSymbol()
{
	return symbol;
}

void Stock::changePrice (double newPrice)
{
	price = newPrice;
}

class Xchange: public Stock
{
public:
	Xchange();
	void addStock (Stock newStock);
	double getPrice (string symbol);
private:
	vector <Stock> stocks;
};

void Xchange::addStock (Stock newStock)
{
	stocks.push_back(newStock);
}

double Xchange::getPrice (string symbol)
{
	for(int i=0; i<stocks.size; i++)
	{
	if (stocks[i].getSymbol==symbol)
		{
		return stocks[i].getPrice();
		}
	}
	return -1;
}

int main()
{
	Xchange nsadaq = new Xchange;
	nsadaq.addStock (Stock("GOOG", 1000));
	cout << nsadaq.getPrice ("GOOG");
}
Line 53:
for(int i=0; i<stocks.size; i++)
change this to:
for(int i=0; i<stocks.size(); i++)

Second, line 65 looks like Java. You don't need the new operator:
Change line 65 from Xchange nsadaq = new Xchange; to Xchange nsadaq;
Thank you for all the help.

I am down to one error

midtermec.cpp: In member function ‘double Xchange::getPrice(std::string)’:
midtermec.cpp:55: error: no match for ‘operator==’ in ‘((Xchange*)this)->Xchange::stocks.std::vector<_Tp, _Alloc>::operator[] [with _Tp = Stock, _Alloc = std::allocator<Stock>](((long unsigned int)i))->Stock::getSymbol == symbol’

I changed everything above as Stewbond recommended.

Any ideas?
Same issue as what you had on line 53. You were calling a function (aka a method) as it it was a variable (aka a member).

Change line 55:
if (stocks[i].getSymbol==symbol)
to
if (stocks[i].getSymbol()==symbol)
i added this

for(int i=0; i<stocks.size(); i++)

from this

for(int=0; i<stocks.size; i++)

now in our unix compiler this is what I am receiving

/tmp/ccRwgLqT.o: In function `Xchange::getPrice(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
midtermec.cpp:(.text+0x18d): undefined reference to `Stock::getPrice()'
/tmp/ccRwgLqT.o: In function `main':
midtermec.cpp:(.text+0x1f3): undefined reference to `Xchange::Xchange()'
collect2: ld returned 1 exit status
FYI, your compiler tells you everything. It's good to learn to decipher these messages.

midtermec.cpp: In member function ‘double Xchange::getPrice(std::string)’:

There's a problem in Xchange::getPrice()

midtermec.cpp:55: error:

This tells you the line where you have a problem

no match for ‘operator==’

There is a problem where the left side cannot be compared to the right side of ==. This usually means that you're calling the wrong type or that you're trying to access something you didn't mean to.

in ‘((Xchange*)this)->Xchange::stocks.std::vector<_Tp, _Alloc>::operator[]
[with
_Tp = Stock,
_Alloc = std::allocator<Stock>
]
(((long unsigned int)i))->Stock::getSymbol == symbol’

For now, ignore this stuff. It's very convoluted, but the previous information gets us very close to the problem.
Last edited on
Thank you Stewbond.

My lab partner and I are in our 2nd quarter of our Computer Science class. So we are below the level of novice.

We both now have the same error

/tmp/ccvQTygC.o: In function `main':
midtermec.cpp:(.text+0x20f): undefined reference to `Xchange::Xchange()'
collect2: ld returned 1 exit status

I'm looking over or main function though and not having any luck.
Got it! Thanks again!
This tells you that you've declared a constructor for Xchange, but you haven't implemented it.

Two solutions:
1. Delete line 39, or
2. Add this in line 45: Xchange::Xchange() { }
Last edited on
Topic archived. No new replies allowed.