Help with code!

Hi, I'm currently doing an assignment using eclipse, when I build the files there is nothing wrong with any file in particular, but the project has an error, here are the files:

// HPStack.h

#ifndef HPSSTACK_H_
#define HPSSTACK_H_


class HPStack {
public:
void push(double newval);
double pop();
double peek();

private:
double x;
double y;
double z;
double t;
};

#endif



//HPStack.cpp


#include "HPStack.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>

using namespace std;



void HPStack::push(double newval){
t = z;
z = y;
y = x;
x = newval;
}

double HPStack::pop(){
double out;
out = x;
x = y;
y = z;
z = t;
return out;
}

double HPStack::peek() {
return x;
}




//Calculator.cpp


#include "HPStack.cpp"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>

#define PI 3.14159

using namespace std;

int main(){
HPStack stack;
string line;
while (getline(cin, line)) {
stringstream ss(line);
string token;
while(ss >> token) {
if (isdigit(token[0])) {
stack.push(atof(token.data()));
}
// a lot of boring else if's here

}
}
cout << stack.peek();
}



Anyways, I'm getting the error message :

/cygdrive/c/Users/Rhys/workspace/Calculator/Debug/../HPStack.cpp:27: multiple definition of `HPStack::peek()'
./Calculator.o:/cygdrive/c/Users/Rhys/workspace/Calculator/Debug/../HPStack.cpp:27: first defined here

the same message is repeated twice more for HPStack::push(double) and HPStack::pop()


Any tips?
Much appreciated


Don't include HPStack.cpp in Calculator.cpp. Instead include HPStack.h
Cool, it turns out it ran anyways, even though there was an error. That did fix the error though, thanks.
I have been trying to get this program to work for days and have come to a point that I can not solve this issue. The program will run but when it gets to the aa_after function the function does not add the word after the word specified it adds it to the first word. The program uses pointers to create a linked list. Any help would be much apreciated. Here is the code:

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAX_WORD_LENGTH = 80;

/* definition of a node */
struct Node;
typedef Node *Node_ptr;

struct Node
{
char word[MAX_WORD_LENGTH];
Node_ptr ptr_to_next_node;
};

/* Function to assign a linked list to "a_node" */
void assign_list(Node_ptr &a_list);

/* Function to assign a new dynamic node variable to "a_node" */
void assign_new_node(Node_ptr &a_node);

/* Function to print the strings in the list "a_node" */
void print_list(Node_ptr a_node);


void add_after(Node_ptr &list, char a_word[], char word_after[]);
//This function inserts a node containing "word_after" in the linked list "list", after the first occurrence of a node containing "a_word". If "list" does not contain such a node, the function leaves it unchanged.


//*****************************************************
void main()
{



char word_a[MAX_WORD_LENGTH];
char aword[MAX_WORD_LENGTH];
Node_ptr my_list = NULL;
assign_list(my_list);
cout << "\nTHE LIST IS NOW:\n";
print_list(my_list);

cout << "After which word would you like to add a word?:";
cin >> word_a;
cout << "What word would you like to add after that word?:";
cin >> aword;
add_after(my_list,aword, word_a);

cout << "The list is now:"<< endl;

print_list(my_list);

system("pause");




}
//*****************************************************

/* DEFINITION OF FUNCTION "assign_list" */
void assign_list(Node_ptr &a_list)
{
Node_ptr current_node, last_node;

assign_new_node(a_list);
cout << "Enter first word (or '.' to end list): ";
cin >> a_list->word;
if (!strcmp(".",a_list->word))
{
delete a_list;
a_list = NULL;
}
current_node = a_list;

while (current_node != NULL)
{
assign_new_node(last_node);
cout << "Enter next word (or '.' to end list): ";
cin >> last_node->word;
if (!strcmp(".",last_node->word))
{
delete last_node;
last_node = NULL;
}
current_node->ptr_to_next_node = last_node;
current_node = last_node;
}
}
/* END OF FUNCTION DEFINITION */

/* DEFINITION OF FUNCTION "assign_new_node" */
void assign_new_node(Node_ptr &a_node)
{
a_node = new Node;
if (a_node == NULL)
{
cout << "sorry - no more memory\n";
exit(1);
}
}
//*****************************************************

/* DEFINITION OF FUNCTION "print_list" */
void print_list(Node_ptr a_node)
{
while (a_node != NULL)
{
cout << a_node->word << " ";
a_node = a_node->ptr_to_next_node;
}
}
//*****************************************************

void add_after(Node_ptr &list, char a_word[], char word_after[])
{


Node_ptr currNode;
Node_ptr newNode;
Node_ptr nextNode;


currNode = list;
while(currNode ->ptr_to_next_node != NULL)
{
if(strcmp(word_after,currNode->word))
{
assign_new_node(newNode);
nextNode = currNode ->ptr_to_next_node;
currNode ->ptr_to_next_node = newNode;
newNode ->ptr_to_next_node = nextNode;
strcpy (newNode ->word,a_word);
break;
}
else
currNode = currNode -> ptr_to_next_node;

}

}
The test of strcmp is incorrect. Strcmp returns 0 (false) if two strings match. Put a "!" in front of the strcmp call.
Ha ha, I have looked at that code dozens of times and each time I thought I had put that exclamation point there! :) Thank you so much for your help!
Topic archived. No new replies allowed.