By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

What's wrong with this program?

P: n/a
Could you tell me what's wrong with this program, it doesn't compile:

#include <iostream>
#include <cstdlib>
using namespace std;

class Everything
{
public:
static Everything* Instance()
{
if(instance==0)
instance=new Everything;
return instance;
}
const long double pi;
const long double e;
const long double phi;
private:
Everything():pi(3.14591),e(2.71828),phi(1.618034){ }
Everything(const Everything&);
Everything& operator=(const Everything&);
private:
static Everything* instance;
};
Everything* Everything::instance = 0;// initialize pointer

Everything* var=Instance();
int main()
{
long double* area(const long double& radius, const Everything* var);
long double rad;
cout << "Enter a radius: ";
cin >> rad;
long double* ret=area(rad,var);
cout << "Area: " << *ret << endl;
delete ret;
system("PAUSE");
return EXIT_SUCCESS;
}
long double* area(const long double& radius, const Everything* var)
{
template<class T>T Exp(const T& base,long long exp);
long double* ret=new long double(var->pi*Exp<long double>(radius,2));
return ret;
}
template<class T>
T Exp(const T& base,long long exp)
{
for(T i=1;exp>0;exp--)
i*=base;
return i;
}

Any suggestions? Thanks!!!

Nov 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Could you tell me what's wrong with this program, it doesn't compile:


First I'll tell you what's wrong with your post. You posted
code, and only say 'it doesn't compile'. I think you've been
posting (and hopefully reading) here long enough to know that
that is not sufficient.

That said, I don't need to tell you what's wrong with the code,
my compiler (and yours) can. Mine says:

Compiling...
cpp.cpp
cpp.cpp(478) : error C2065: 'Instance' : undeclared identifier
cpp.cpp(478) : error C2440: 'initializing' : cannot convert from 'int' to
'class Everything *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
cpp.cpp(493) : error C2951: template declarations are only permitted at
global or namespace scope
cpp.cpp(493) : error C2143: syntax error : missing ';' before 'newline'
cpp.cpp(493) : error C2632: 'long' followed by 'long' is illegal
cpp.cpp(494) : error C2065: 'Exp' : undeclared identifier
cpp.cpp(494) : error C2062: type 'long double' unexpected
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : error C2143: syntax error : missing ';' before '}'
cpp.cpp(496) : fatal error C1003: error count exceeds 100; stopping
compilation
Error executing cl.exe.

cpp.obj - 102 error(s), 0 warning(s)

What to do:

while(errors)
{
fix_first_error();
recompile();
}
-Mike
Nov 22 '05 #2

P: n/a
OK, I fixed some of the errors, but it still won't work. And how is
instance undeclared?

Code:

template<class T>
T Exp(const T& base,long long exp)
{
T i;
for(i=1;exp>0;exp--)
i*=base;
return i;
}

long double* area(const long double& radius, const Everything* var)
{
long double* ret=new long double(var->pi*Exp<long double>(radius,2));
return ret;
}

class Everything
{
public:
static Everything* Instance()
{
if(instance==0)
instance=new Everything;
return instance;
}
const long double pi;
const long double e;
const long double phi;
private:
Everything():pi(3.14591),e(2.71828),phi(1.618034){ }
Everything(const Everything&);
Everything& operator=(const Everything&);
private:
static Everything* instance;
};
Everything* Everything::instance = 0;// initialize pointer

Everything* var=Instance();
int main()
{
cout << "Enter a radius: ";
cin >> rad;
long double* ret=area(rad,var);
cout << "Area: " << *ret << endl;
delete ret;
system("PAUSE");
return EXIT_SUCCESS;
}

Could you tell me *exactly* hat's wrong, b/c my debugger is broken?
Thanks!!!

Nov 22 '05 #3

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
OK, I fixed some of the errors, but it still won't work. And how is
instance undeclared?


Instance() is a function declared inside your class.
Everything* var=Instance(); is trying to use a global Instance() which
doesn't exist.
That is how it's undeclared.
Nov 22 '05 #4

P: n/a

Jim Langston wrote:
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
OK, I fixed some of the errors, but it still won't work. And how is
instance undeclared?


Instance() is a function declared inside your class.
Everything* var=Instance(); is trying to use a global Instance() which
doesn't exist.
That is how it's undeclared.


How? It's static!!!!

Nov 22 '05 #5

P: n/a
Never mind, I fixed that, now its saying that var is undeclared in the
area function and that's there's a missing semicolon and stuff.
Here's the code:

#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
T Exp(const T& base,long long exp)
{
T i;
for(i=1;exp>0;exp--)
i*=base;
return i;
}

long double* area(const long double& radius, const Everything* var)
{
long double* ret=new long double((var->pi)*Exp<long double>(radius,2));
return ret;
}

class Everything
{
public:
static Everything* Instance()
{
if(instance==0)
instance=new Everything;
return instance;
}
const long double pi;
const long double e;
const long double phi;
private:
Everything():pi(3.14591),e(2.71828),phi(1.618034){ }
Everything(const Everything&);
Everything& operator=(const Everything&);
private:
static Everything* instance;
};
Everything* Everything::instance = 0;// initialize pointer

Everything* var=Everything::Instance();
int main()
{
cout << "Enter a radius: ";
long double rad;
cin >> rad;
long double* ret=area(rad,var);
cout << "Area: " << *ret << endl;
delete ret;
system("PAUSE");
return EXIT_SUCCESS;
}

Any suggestions? Thanks!!!

Nov 22 '05 #6

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Never mind, I fixed that, now its saying that var is undeclared in the
area function and that's there's a missing semicolon and stuff.
Here's the code:

#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
T Exp(const T& base,long long exp)
{
T i;
for(i=1;exp>0;exp--)
i*=base;
return i;
}

long double* area(const long double& radius, const Everything* var)
{
long double* ret=new long double((var->pi)*Exp<long double>(radius,2));
return ret;
}

class Everything
{
public:
static Everything* Instance()
{
if(instance==0)
instance=new Everything;
return instance;
}
const long double pi;
const long double e;
const long double phi;
private:
Everything():pi(3.14591),e(2.71828),phi(1.618034){ }
Everything(const Everything&);
Everything& operator=(const Everything&);
private:
static Everything* instance;
};
Everything* Everything::instance = 0;// initialize pointer

Everything* var=Everything::Instance();
int main()
{
cout << "Enter a radius: ";
long double rad;
cin >> rad;
long double* ret=area(rad,var); I see var here, where is it declared?
cout << "Area: " << *ret << endl;
delete ret;
system("PAUSE");
return EXIT_SUCCESS;
}

Any suggestions? Thanks!!!


Yes, read your error messages and fix whey they tell you is broken.
Nov 22 '05 #7

P: n/a
Protoman wrote:
long double* area(const long double& radius, const Everything* var)
{
long double* ret=new long double((var->pi)*Exp<long double>(radius,2));
return ret;
}

class Everything
{


The function "area" uses "Everything" and derefernces a variable of
that type without telling what "Everything" is. This is precisely what
the compiler complains about. Move the definition of "area" below the
definition of "Everything".

Nov 22 '05 #8

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...

Jim Langston wrote:
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
> OK, I fixed some of the errors, but it still won't work. And how is
> instance undeclared?


Instance() is a function declared inside your class.
Everything* var=Instance(); is trying to use a global Instance() which
doesn't exist.
That is how it's undeclared.


How? It's static!!!!


That's completely irrelevant. Go back to your textbook
and read about 'scope'.

-Mike
Nov 23 '05 #9

P: n/a

Protoman wrote:
Jim Langston wrote:
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
OK, I fixed some of the errors, but it still won't work. And how is
instance undeclared?


Instance() is a function declared inside your class.
Everything* var=Instance(); is trying to use a global Instance() which
doesn't exist.
That is how it's undeclared.


How? It's static!!!!


You need to give it scope:

Everything::Instance() is the correct way to call that funct.

Nov 23 '05 #10

P: n/a
"Protoman" <Pr**********@gmail.com> writes:
Never mind, I fixed that, now its saying that var is undeclared in the
area function and that's there's a missing semicolon and stuff.
Here's the code:

#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
T Exp(const T& base,long long exp)


When getting loads of errors, the best recipie is usually to fix
the first one, and then recompile. I tried your source, and am
willing to help you with the first one:

protoman.cc:7: error: ISO C++ does not support `long long'

The best way to fix a compile error is not to post garbage code to
clc++, but to read the error message, try to understand what
it says, then try to understand why it says so, and finally
fix the corresponding line of code.

Step one: "protoman.cc:7: error: ISO C++ does not support `long long'"

This means that there is something with the file 'protoman.cc'
(which is what I named it on my computer, it might be something
else on your).

That something happens at (or near) line 7, and it is an 'error'
(which is my compiler's way of saying: "I refuse to output any
result (other than compiler messages)". It could have said
warning, which would have meant: "Maybe an error, you decide".

The message is "ISO C++ does not support `long long'"

This means that there is no type named long long in C++.
The line in question reads: "T Exp(const T& base,long long exp)"

At the end immediatly before the token 'exp' it says long long.
The obvious way to fix this is to change either to a type that
exists in the language, or to a user defined type.

Now fix this, recompile, and then repeat the process until there
are no errors left.

If there is some specific error message you really don't understand,
post that message together with a minimal code sample that produce
that error, and you might find some more help here, or perhaps you'll
find out what it means in the process of minimizing the code sample.

By the way, please post properly indented code next time.

/Niklas Norrthon
Nov 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.