Reputation: 165
New user, new to programming, x seconds of search didn't find me an answer. Whole code (incomplete - and an online quiz for basic stuff):
#include "stdafx.h"
#include <iostream>
#include "constant.h"
double towerheight()
{
std::cout << "Input tower height" << std::endl;
double height;
std::cin >> height;
return height;
}
double ballheight(double towerheight)
{
//valid stuff goes here in program, but not applicable to question
}
int main()
{
double towerheight;
towerheight = towerheight(); //Error occurs here
ballheight(towerheight);
return 0;
}
3 lines from bottom ignoring whitespace and closing bracket is where the error occurs. (term does not evaluate to a function taking 0 arguments.)
I'm sort of lost at the research end of this and could use some human help.
Apologies for potentially poor formatting. Please advise, I may not know certain terms responsible for the error, and my goal is to understand what's going on.
EDIT:
Solution for me was changing the name of the double variable above the error. Why did this become an issue?
Upvotes: 3
Views: 2942
Reputation: 1040
The name of the variable is the same name of the function you are calling. You can change eighter the name of the variable or the name of the function. Usually when you are using C++ and declaring variables is a good practice to initialize the value of the double to something like 0.0.
And you are using 2 lines: 1 for declare the variable and another to give it the value when you can perfectly do this in one line. Better code uses less lines always keep it in mind.
This was my approach:
#include "stdafx.h"
#include <iostream>
#include "constant.h" //comented
double Gettowerheight()
{
std::cout << "Input tower height" << std::endl;
double height;
std::cin >> height;
return height;
}
double ballheight(double towerheight)
{
return 0.0;
//valid stuff goes here in program, but not applicable to question
}
int main()
{
double towerheight = Gettowerheight();
ballheight(towerheight);
system("pause");
return 0;
}
I put a return in the ballheight to compile the code as well as comment the 3rd include "constant.h"
Tested on Visual Studio 2013
Upvotes: 1
Reputation: 206667
In main
, the variable named towerheight
shadows the function towerheight
. Hence,
towerheight = towerheight();
is not valid. It is analogous to calling a function on a variable.
double towerheight;
double dummy;
towerheight = dummy();
That's why it is not correct.
Solution 1
You can use:
double towerheight;
towerheight = ::towerheight();
Using ::towerheight
works since it refers to the name in the enclosing namespace and not the locally defined variable of the same name.
Solution 2
Use different names.
You can avoid some of the confusion by using two different names.
double t_height = towerheight();
Upvotes: 2