I am getting the error "error C2228: left of '.push' must have class/struct/union" when I try to push a random double onto the stack.

Here's my driver...

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "DynStack.h"
using namespace std;

int main()
{
	double dblCatch;		//holds values popped off the stack
	float floatCatch;			//holds values popped off the stack
	int size;				//size of a stack

	//create two different DynStack objects with different data types
	DynStack<double> doubleStack();
	DynStack<float> floatStack();

	cout << "Enter number of doubles to put in the stack: ";
	cin >> size;

	for (int count = 0; count < size; count++)
	{
		srand (time(NULL));
		doubleStack.push((double)rand() / ((double)(RAND_MAX)+(double)(1)));
	}

	return 0;
}

Here's my DynStack header...

#ifndef DYNSTACK_H
#define DYNSTACK_H
#include <iostream>
#include <cstdlib>
using namespace std;

template <class T>
class DynStack
{
private:
	//structure for stack nodes
	struct StackNode
	{
		T value;			//value in the node
		StackNode *next;	//pointer to the next node
	};

	StackNode *top;			//pointer to the stack top;

public:
	//constructor
	DynStack()
	{
		top = NULL;
	}

	//destructor
	~DynStack();

	//stack operations
	void push(T);
	void pop(T &);
	void isEmpty();
};
#endif
DynStack<double> doubleStack();
DynStack<float> floatStack();

C++ syntax is kind of weird here. The compiler thinks you are trying to declare two functions, not instantiate two objects. For named objects, the default constructor does not need parentheses, and the syntax no longer looks like a function:

DynStack<double> doubleStack;
DynStack<float> floatStack;

Yeah I orginally had it coded that way but I was getting an unresolved external variable error, so I threw the parantheses on and it compiled fine... until I tried to push something on the stack.

This is the error I will get if I leave off the parantheses.

error LNK2019: unresolved external symbol "public: __thiscall DynStack<double>::~DynStack<double>(void)"

You didn't implement the destructor or any of the other methods in the DynStack class. The only method with a body is the default constructor.

It's all in my .cpp file. It's worked for other programs I don't know why it wouldn't work here.

#include <iostream>
#include <new>
#include "DynStack.h"
using namespace std;


//destructor
template <class T>
DynStack<T>::~DynStack()
{
	StackNode *nodePtr, *nextNode;

	//position nodePtr at the top of the stack
	nodePtr = top;

	//traverse the list deleting each node
	while (nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;
	}
}



//push
template <class T>
void DynStack<T>::push(T val)
{
	StackNode *newNode;		//pointer to a new node

	//allocate a new node and store val there
	newNode = new StackNode;
	newNode->value = val;

	//if there are no nodes in the list
	if (isEmpty())
	{
		top = newNode;
		newNode->next = NULL;
	}
	else	//otherwise, insert newNode before top
	{
		newNode->next = top;
		top = newNode;
	}
}



//pop
template <class T>
void DynStack<T>::pop(T &val)
{
	StackNode *temp;		//temporary pointer

	//make sure the stack is not empty
	if (isEmpty())
	{
		cout << "The stack is empty.\n";
	}
	else	//pop value off of stack
	{
		val = top->value;
		temp = top->next;
		delete top;
		top = temp;
	}
}

It's worked for other programs I don't know why it wouldn't work here.

In the other programs you weren't working with a template class. The whole definition of the template class has to be in one file, you can't split it up without using the export keyword. Unfortunately, export is not supported by many compilers. Check your compiler's documentation to see if it allows export, otherwise you need to stuff everything in the header.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.