#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <fstream>
usingnamespace std;
int numhours(int hour){
int time[3]={0,0,0};
while(hour<1||time[2]==0){
if(hour<1){
cout <<"new values";
cin >> hour;
}else{
for (;;){
if(hour<=168){
hour-=168;
time[0]++;
} elseif (hour < 24){
hour -=24;
time[1]++;
} else {
time[2]=hour;
hour=0;
}
if (hour ==0){
return time;
}
}
}
}
}
int main (void) {
// INSERT YOUR VARIABLE DECLARATIONS HERE
int *h;
h=numhours(218);
cout <<endl<<" "<<h[0] <<" "<<h[1]<<" "<<h[2]<< endl;
system("PAUSE"); return 0;
}
my errors say:
In function 'int numhours(int)':
Line 33 col 12 [Error] invalid conversion from 'int*' to 'int' [-fpermissive]
Line 10 col 9 [Warning] address of local variable 'time' returned [-Wreturn-local-addr]
In function 'int main()':
LIne 46 Col 15 [Error] invalid conversion from 'int' to 'int*' [-fpermissive]
The invalid conversion int* to int highlights the return time; in my function as the error.
the warning highlights highlights my int time[3]={0,0,0};
The final invalid conversion int* to int highlights the h=numhours(218); in my main as the error.
#include <iostream>
#define SINGLETON(class_name) \
public: \
static class_name& get() \
{ \
static class_name singleton;\
return singleton; \
} \
private: \
class_name() {} \
class_name(const class_name&) = delete; \
void operator=(const class_name&) = delete; \
public:
class Base
{
public:
virtualvoid print() = 0;
};
class Singleton1 : public Base
{
SINGLETON(Singleton1)
public:
void print() { std::cout << "you can only have 1 instance of this class 1" << std::endl; }
};
class Singleton2 : public Base
{
SINGLETON(Singleton2)
public:
void print() { std::cout << "you can only have 1 instance of this class 2" << std::endl; }
};
class Singleton3 : public Base
{
SINGLETON(Singleton3)
public:
void print() { std::cout << "you can only have 1 instance of this class 3" << std::endl; }
};
int main()
{
Base* base(&Singleton1::get());
base->print();
base = &Singleton2::get();
base->print();
base = &Singleton3::get();
base->print();
}
You can't return a pointer or reference to a local variable.
Gamer2015 wrote:
Why not?
It seems to work...
Typically when someone says "local variable" in the context of a function they are referring to a variable with automatic storage duration. Variables with static storage duration still exist when the function isn't being executed (provided, of course, the function has been called at least once,) so they aren't really 'local' to the function in terms of duration/accessibility.
Typically when someone says "local variable" in the context of a function they are referring to a variable with automatic storage duration. Variables with static storage duration still exist when the function isn't being executed (provided, of course, the function has been called at least once,) so they aren't really 'local' to the function in terms of duration/accessibility.
Ah okey :)
For some reason the compiler only gives a warning :o
Note: The result is undefined behaviour but i just wanted to show that it compiles :o