Sign in to follow this  
bxela1

Frustrated

Recommended Posts

bxela1    130
I recently added classes to my practice code and I get 3 errors, I have checked through my book and can't find why I get the errors. It's probably a really noobie mistake but it's driving me crazy. The errors are error C2628: 'test' followed by 'int' is illegal (did you forget a ';'?) error C2556: 'test test::testx(int)' : overloaded function differs only by return type from 'int test::testx(int)' : see declaration of 'test::testx' : error C2371: 'test::testx' : redefinition; different basic types : see declaration of 'test::testx' and the code is
#include <iostream>
#include <string>
using namespace std;
double &f();
double val = 10;
class test {
	int a;
	int b;
public:
	test();
	int testy(int e);
	int testx(int i);
	int geta();
	int getb();
}

int test::testx(int i)
{
    a=a*i;
}

test::test()
{
	a=0;
	b=0;
	cout << "Class created" << endl;
}

int test::testy(int e)
{
	b=e*e;
}

int test::geta()
{
	return a;
}

int test::getb()
{
	return b;
}

int main()
{
	test v;
	int i;
	int target;
	string word;
	string *wordpoint = &word;
	int *pointer = &target;
	(*pointer) = 25;
	(*wordpoint) = "is target's value";
	cout << target << word << endl;
	cout << f() << " is the current value of val" << endl;
	f() = f() * 10;
	cout << f() << " is the new value of val" << endl;
	cout << "Enter i's value: ";
	cin >> i;
	cout << "i times a = " << v.geta() << endl;
	cout << "i times i = " << v.getb() << endl;
	return 0;
}

double &f()
{
	return val;
}

thx in advance

Share this post


Link to post
Share on other sites
MaulingMonkey    1728
Glare Biggest newbie mistake was stripping out the line numbers for those error messages.

}; <--- this is missing at the end of the class definition

int test::testx(int i) <--- where the (nonsensical) errors start --- in other words, where the compiler has gotten confused


There are other errors reported by VS2005 after fixing this one, but these are obvious and easily fixed.

Share this post


Link to post
Share on other sites
bxela1    130
After I fixed it it turned out that a had no value.

After a few mins of moving around of code I came to an end result.


#include <iostream>
#include <string>
using namespace std;
double &f();
double val = 10;
class test {
int a;
int b;
public:
test();
int testy(int e);
int testx(int i);
void geta();
};

void test::geta()
{
cout << "Enter a's value: ";
cin >> a;
}

int test::testx(int i)
{
a=a*i;
return a;
}

test::test()
{
a=0;
b=0;
cout << "Class created" << endl;
}

int test::testy(int e)
{
b=e*e;
return b;
}


int main()
{
test v;
int i;
int e;
int target;
string word;
string *wordpoint = &word;
int *pointer = &target;
(*pointer) = 25;
(*wordpoint) = "is target's value";
cout << target << word << endl;
cout << f() << " is the current value of val" << endl;
f() = f() * 10;
cout << f() << " is the new value of val" << endl;
v.geta();
cout << "Enter i's value: ";
cin >> i;
cout << "Enter e's value: ";
cin >> e;
int testx(int i);
int testy(int e);
cout << "i times a = " << v.testx(i) << endl;
cout << "e times e = " << v.testy(e) << endl;
return 0;
}

double &f()
{
return val;
}



thx again [lol]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this