C++ error C2106 and C2448

Trying to use a function with a pointer argument

Page 1 of 1

10 Replies - 1314 Views - Last Post: 26 April 2010 - 11:10 PM Rate Topic: -----

#1 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

C++ error C2106 and C2448

Posted 22 April 2010 - 11:04 AM

Hello,

I'm working on this assignment for my class for hours, and now I'm down to 4 errors. One error says "left operand must be l-value" and three others say "function-style initializer appears to be a function definition." We're supposed to use a function with a function pointer argument so that we can pass it to another function. Any help with this is greatly appreciated!!! (and I'm sorry if I didn't post the code right, I'm new to this)
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
bool getChoice();
bool quit(char choice);
int square();
int cube();
int fourthPower();
char choice = ' ';
int *n1, *n2;

void main()
{
	cout << "Enter the first number for the interval." << endl;
	cin >> *n1;
	while (*n1 < 0 || *n1 > 100)
	{
		cout << "ERROR. Please enter a number 0 through 100." << endl;
		cin >> *n1;
	}
	cout << "Enter the second number for the interval." << endl;
	cin >> *n2;
	while (*n2 < 0 || *n2 > 100)
	{
		cout << "ERROR. Please enter a number 0 through 100." << endl;
		cin >> *n2;
	}
}
bool getChoice()
{
	cout << "S" << setw(3) << "Square"
	<< "\t\tC" << setw(3) << "Cube" << endl;
	cout << "F" << setw(3) << "Fourth Power"
	<< "\t\tQ" << setw(3) << "Quit" << endl;

	cout << "Choose the option in which you would like to proceed with: " << endl;
	cin >> choice;
	switch (choice)
	{
		case 'Q':
		case 'q':
			return true;
			break;
		case 'S':
		case 's':
			return true;
			break;
		case 'C':
		case 'c':
			return true;
			break;
		case 'F':
		case 'f':
			return true;
			break;
		default:
			cout << "Please enter a valid choice.";
	}
}

bool quit(char choice)
{
	if (choice = 'Q' || choice = 'q')
	{
		cout << "You chose to quit." << endl;
		return true;
	}
	else
		return false;
}
int square(n1, n2)
{
	if (&n1 ==) 0 || (&n2 == 0)
		return 1;
	else
		return square (&n1, 2);
}
int cube(n1, n2)
{
	if (&n1 == 0 || &n2 == 0)
		return 1;
	else
		return cube (&n1, 3);
}
int fourthPower(n1, n2)
{
	if (&n1 == 0 || &n2 == 0)
		return 1;
	else
		return fourthPower (&n1, 4);
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ error C2106 and C2448

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ error C2106 and C2448

Posted 22 April 2010 - 11:29 AM

1) So what's the problem you face specifically? Those errors are in English. You can Google/read, think, and fix code...
2) Don't describe errors. Copy paste. If you have not copy pasted before, here are instructions. Move mouse to beginning of text. Hold left mouse button. Drag to select all text. Release button. Press ctrl-c on keyboard. Paste in post here by using ctrl-v.
Was This Post Helpful? 0
  • +
  • -

#3 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: C++ error C2106 and C2448

Posted 24 April 2010 - 09:07 AM

I'm just not sure why I'm getting those errors. I'm still working on this, but I wanted to fix these errors before moving on. Sorry if I'm not posting questions right, I'm new and also very new to C++ coding.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: C++ error C2106 and C2448

Posted 24 April 2010 - 09:11 AM

Answer this: what are n1 and n2 pointing to?

Then answer this: where is the value the user inputting being stored?

Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ error C2106 and C2448

Posted 24 April 2010 - 09:16 AM

Quote

I'm just not sure why I'm getting those errors.
You're getting those errors because you made syntactical mistakes.

Quote

I'm new and also very new to C++ coding.
I know. But it's not about knowledge. It's about how to tackle problems. Imagine coming to a fork in the road. You don't know whether to go left or right. I'm trying to get you to be able to make that decision by yourself. Programming is all about problems. There's a mentality needed to solve those problems.

EDIT: Ahh, that's right, spoiler tags! I'm going to start gleefully using them from now on!
Was This Post Helpful? 0
  • +
  • -

#6 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: C++ error C2106 and C2448

Posted 24 April 2010 - 09:21 AM

OK, thanks, I'll look at this program again and try to figure it out. If I come up with anything else afterwards (which is probable), I'll come back.
Was This Post Helpful? 0
  • +
  • -

#7 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: C++ error C2106 and C2448

Posted 26 April 2010 - 09:41 PM

It's been a couple of days, but I changed my code a bit and am down to 3 errors. My code is:
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
bool getChoice();//validates choice entered by user
bool quit(char choice);//quits program when quit is chosen
int square(int *n1, int *n2);//squares the interval of numbers entered
int cube(int *n1, int *n2);//cubes the interval of numbers entered
int fourthPower(int *n1, int *n2);//puts the interval of numbers entered to the fourth power
char choice = ' ';//choice of action to take by user
int *n1, *n2;//two numbers entered by user to use as an interval

int main()
{
	cout << "Enter the first number for the interval." << endl;
	cin >> *n1;
	while (*n1 < 0 || *n1 > 100)
	{
		cout << "ERROR. Please enter a number 0 through 100." << endl;
		cin >> *n1;//validates that the first number is between 0 and 100
	}
	cout << "Enter the second number for the interval." << endl;
	cin >> *n2;
	while (*n2 < 0 || *n2 > 100)
	{
		cout << "ERROR. Please enter a number 0 through 100." << endl;
		cin >> *n2;//validates that the second number is between 0 and 100

		return 0;
	}
}
bool getChoice()
{
	cout << "S" << setw(3) << "Square"
	<< "\t\tC" << setw(3) << "Cube" << endl;
	cout << "F" << setw(3) << "Fourth Power"
	<< "\t\tQ" << setw(3) << "Quit" << endl;
	//Table of valid choices for user to choose from

	cout << "Choose the option in which you would like to proceed with: " << endl;
	cin >> choice;
	switch (choice)
	{
		case 'Q':
		case 'q':
			return quit;
			break;
		case 'S':
		case 's':
			return square;
			break;
		case 'C':
		case 'c':
			return cube;
			break;
		case 'F':
		case 'f':
			return fourthPower;
			break;
		default:
			cout << "Please enter a valid choice.";
			//makes sure entered character is valid
	}
}

bool quit(char choice)
{
	if (choice != 'Q' || choice != 'q')
	{
		cout << "You chose not to quit." << endl;
		return true;
		//continues with program if quit is not chosen
	}
	else
	{
		cout << "You chose to quit." << endl;
		return false;
		exit(0);
	}
		//quits program if quit is chosen
}
int square(int *n1, int *n2)
{
	if (&n1 == 0 || &n2 == 0)
		return 1;
	else
		return &n1 * square (&n1, 2);
		//squares all the numbers in the interval
}
int cube(*n1, *n2)
{
	if (&n1 == 0 || &n2 == 0)
		return 1;
	else
		return &n1 * cube (&n1, 3);
		//cubes all the numbers in the interval
}
int fourthPower(*n1, *n2)
{
	if (&n1 == 0 || &n2 == 0)
		return 1;
	else
		return &n1 * fourthPower (&n1, 4);
		//puts all numbers in the interval to the fourth power
}

The errors are:
.\Assignment2Problem1.cpp(88) : error C2664: 'square' : cannot convert parameter 1 from 'int **' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\Assignment2Problem1.cpp(92) : error C2448: 'cube' : function-style initializer appears to be a function definition
.\Assignment2Problem1.cpp(100) : error C2448: 'fourthPower' : function-style initializer appears to be a function definition
Build log was saved at "file://c:\Documents and Settings\Owner.YOUR-ABDCCD4FD6\My Documents\CS 170\Assignment2Problem1\Assignment2Problem1\Debug\BuildLog.htm"
Assignment2Problem1 - 3 error(s), 8 warning(s)
I looked up the first error for answers and didn't really find what I was looking for. With the other 2 errors, what I found didn't answer my question either (it said to include the argument type, but when I do that, it returns the first error, if that makes sense). If anyone could just tell me what it is I'm doing wrong, that would be awesome!
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ error C2106 and C2448

Posted 26 April 2010 - 10:01 PM

Again, you need to read the error. Here, as an example.

Quote

.\Assignment2Problem1.cpp(88) : error C2664: 'square' : cannot convert parameter 1 from 'int **' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
What does this mean?

Assignment2Problem1.cpp(88) So this means line 88. What's on line 88? return &n1 * square (&n1, 2); Interesting. What's wrong here?

error C2664: 'square' Something involving square. Let's focus on that part.

cannot convert parameter 1 Even more specific. parameter 1, if you understand the concepts of parameters, is &n1

cannot convert parameter 1 from 'int **' to 'int *' Why does it need to convert? Let's check what the type is supposed to be.

int square(int *n1, int *n2)



So the type is supposed to be int*. Are you OK with this or not?

Furthermore, you have square (&n1, 2), and in particular, &n1. What is the type of &n1?
Was This Post Helpful? 0
  • +
  • -

#9 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: C++ error C2106 and C2448

Posted 26 April 2010 - 10:17 PM

I believe I followed everything you said. Yes, the type should be int*. The type of &n1 is also int.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ error C2106 and C2448

Posted 26 April 2010 - 11:03 PM

Quote

The type of &n1 is also int
How sure are you? Stop and think. The error says there's a conversion from 'int **' to 'int *'. We know that n1 is int*. to int* makes sense. So that would mean &n1, is int **.

Maybe you are confused about the & operator. Maybe you should go back and read about it.

And not only that, you should think about what you are writing. if square takes a pointer as the first parameter, then you need to ensure you pass a pointer when calling it. Not just a pointer, but a meaningful one.
Was This Post Helpful? 0
  • +
  • -

#11 xsunkisses44x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-April 10

Re: C++ error C2106 and C2448

Posted 26 April 2010 - 11:10 PM

I will look into the & operator and I get what you were saying. I'm clearly having a hard time with this, but I'm determined to get it right. Thanks for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1