What's wrong with this program?

P

Protoman

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!!!
 
M

Mike Wahler

Protoman said:
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
 
P

Protoman

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!!!
 
R

red floyd

Protoman said:
OK, I fixed some of the errors, but it still won't work. And how is
instance undeclared?

[redacted]

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

You have an error on line 42. In other words. What is the input, what
is the expected output, what output are you receiving?

We're not psychic, you know.
 
J

Jim Langston

Protoman said:
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.
 
P

Protoman

Jim said:
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!!!!
 
P

Protoman

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!!!
 
J

Jim Langston

Protoman said:
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.
 
N

Neelesh Bodas

Protoman said:
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".
 
N

Niklas Norrthon

Protoman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top