0

I preempt my question with the fact that I started using C++ two days ago.

I get this error: "a value of type "double (*)()" cannot be assigned to an entity of type "double""

My pertinent code:

#include "stdafx.h" #include <iostream> #include <string> using namespace std; double getInput() { double val1 = 0.00; // prompts user for a Double value cin >> "Enter a Double value: " >> val1; return val1; } 

Within main() I have the following statements whereby the equals sign is underlined in red and yields the above provided error message.

double var1; var1 = getInput; 

I must be making a silly mistake, as this seems as simple a programming task as they get, but I did preempt my question ;-) Thanks for any/all help! Additional Information:

  1. Using Visual Studio 2013 Ultimate
  2. This is a Console application
1
  • 3
    Just call your function as var1 = getInput(); Commented Feb 21, 2015 at 2:10

2 Answers 2

1

getInput is the name of a function. That name by itself refers to the function, but it doesn't call the function. And since a function name is, in most contexts, implicitly converted to a pointer to the function, it becomes an expression of type double (*)(), or "pointer to function (with no parameters) returning double".

To call a function, you need to follow its name with an argument list in parentheses. If there are no arguments, use empty parentheses.

Change this:

var1 = getInput; 

to this:

var1 = getInput(); 

(There are languages in which parameterless function calls don't use empty parentheses. C++ isn't one of them.)

Sign up to request clarification or add additional context in comments.

Comments

1

There are two issues in your code snippet:

cin >> "Enter a Double value: " >> val1; 

This line do not compile operator>> is not overloaded for const char*. This should be corrected as follows:

cout<<"Enter a Double value: " << std::endl; cin>>val1; 

Second getInput is a function and its type is double() and it is different from type double. so assignment should be corrected as follows in snippet.

 int main() { double var1; var1 = getInput(); std::cout<< var1<<std::endl; return 0; } 

2 Comments

Could you please edit in an explanation of why/how this code answers the question? Code-only answers are discouraged, because they are not as easy to learn from as code with an explanation. Without an explanation it takes considerably more time and effort to understand what was being done, or the changes made to the code. The explanation is important both for people attempting to learn from the answer and those evaluating the answer to see if it is valid, or worth up voting.
I've yet to determine how to give a"thumbs up " here, but all three replies perfectly explained my error(s). I'm replying from my cell phone and thus will add an explanation specifying what my goal is in my snippet later. I sincerely thank all three of you for your help! Loving this forum already; Glenn

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.