class problems

Alright I am doing a project for different class and I think I have everything good but I cant get this line of code to get past without giving the error code about overloading a function

TestMenu.cpp
1
2
3
4
5
6
7
8
 #include "MenuBuilder.h"
#include "MenuBuilder.cpp"


int main()
{
   checkBal();
}


MenuBuilder.cpp
1
2
3
4
5
6
#include "Menubuilder.h"

void checkBal()
{
	std::cout<<"Current balance is: $"<<balance<<std::endl;
}


MenuBuilder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>

namespace {

double balance =2439.45;
double Amount;
double Ebalance;

void checkBal();
double withD();
double Depo();
void AccountI();
void VeiwState();
void BankI();
void Exit();
}
closed account (Dy7SLyTq)
you need to give the namespace a name then you need to do in the main nameofyournamespace::checkBal();
and in your .cpp file void nameofyournamespace::checkbal()
1
2
3
4
5
6
7
8
9
#include "MenuBuilder.h"
#include "MenuBuilder.cpp"

using namespace std;

int main()
{
   checkBal();
}
1
2
3
4
5
6
7
8
#include "Menubuilder.h"

using namespace std;

void checkBal()
{
	cout<<"Current balance is: $"<<balance<<endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MenuBilder_H_
#define MenuBuilder_H_

#include <iostream>
#include <iomanip>

using namespace std;

double balance = 2439.45;
double Amount;
double Ebalance;

void checkBal();
double withD();
double Depo();
void AccountI();
void VeiwState();
void BankI();
void Exit();

#endif 


Alright now I am getting the following errors
1
2
3
4
error C2374'balance' : redefinition; multiple initialization
error C2088 '<<': illegal for class
error C2086' double Ebalnce': redefinition
error C2086 'double Amount':redefinition


I have triedto change the doubles to something else and as far as i know the << is the only way to isplay something to the screen using cout system
Most likely, in one or more of the functions you didn't post you wrote something like
double balance; // more than once
The complete error message should tell you which file and which line the error is in. From there just call it balance and the first error should go away.
Actually, its because "balance" is being defined in multiple files, due to the header file being included in two different .cpp files. What you may want to do instead is something like this:
1
2
3
4
// extern tells the compiler that it can look in another file
extern double balance;
extern double Amount;
extern double Ebalance;


And then simply make sure you remember to initialize balance somewhere in another file.
Topic archived. No new replies allowed.