error in functon definition or declaration; function not called

hey guys,
i wrote this code and i have an error that i really couldnt understand i think it is in line 16 in bin.cpp
here is the code :
this is the header var.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
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef VAR_H
#define VAR_H

#include <iostream>
#include <cstring>
using namespace std;
class variable;
typedef enum
{

	uu,
	tt,
	ff,
}trivalue;

class formula
{

public:

	trivalue value;

	formula ();
	void setvalue (trivalue v);
	virtual void print (ostream & os){
	};

};

class variable :public formula{

	char name[8];
	variable (char * name);
	variable ();
	void setName (const char * aName);
	void print (ostream & os);

};




#endif  


this is the source file var.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
#include "var.h"

formula::formula()
{

	value=uu;
};

void formula::setvalue (trivalue v)
{

	value=v;

};

variable::variable ():formula()
{
	
	strncpy_s (name," ",8);
}
void variable::setName (const char *aName){

	strncpy_s(name,aName,8);
};


void variable::print (ostream & os){

	os << name;
}


this is the header file bin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef BIN_H
#define BIN_H
#include "var.h"

class binaryOperations : public formula {

	formula left;
	formula right;
	binaryOperations (formula & l, formula & r);
	virtual void printSeparator (ostream & os){}
	void print (ostream & os);
};

#endif 



this is the source file bin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "bin.h"

binaryOperations::binaryOperations (formula & l, formula & r):left(l), right(r)
{	
}
void binaryOperations::printSeparator (ostream & os){

	
}


void binaryOperations::print (ostream & os){

	os << "(";
	left.print(os);
	printSeparator (os);
	right.print (os);
	os << ")";
}


and these are the two errors am getting
1>c:\users\admin\documents\visual studio 2008\projects\lab 9\lab 9\bin.cpp(6) : error C2084: function 'void binaryOperations::printSeparator(std::ostream &)' already has a body
1>        c:\users\admin\documents\visual studio 2008\projects\lab 9\lab 9\bin.h(10) : see previous definition of 'printSeparator'

THANK YOU :)!!!
Because you gave it a body twice, albeit the same body. You defined a body in the bin.h file, then you defined it again in the bin.cpp file.
You are trying to implement printSeparator() in two locations, in the class definition and out side the class definition. You can only implement the same function once.
Notice the {} on line 10 of bin.h: there you already defined the body of printSeperator. Remove that braces, then the error will be away.
i removed it and it worked thank you all :) but one question, i did the exact same thing in var.h and var.cpp. i gave the function two bodies. can you tell me what is the difference between both cases? thanks again guys :D
I don't see anywhere in the variable class where you supplied two bodies.

Also just a nit, but you should be consistent about your brace placement. Sometimes you place it on the same line as the statement other times you place it on it's own line, be consistent.

Also you shouldn't be using semicolons after the closing brace of your functions, their unnecessary and some compilers will generate errors if they are present.

1
2
3
4
5
6
7
8
9
variable::variable ():formula()
{
	
	strncpy_s (name," ",8);
}
void variable::setName (const char *aName){

	strncpy_s(name,aName,8);
};


Last edited on
thanks :) i think i found out what i want. as for the braces i'll make sure i write my code in better style next time
Topic archived. No new replies allowed.