need help with my code

Apr 10, 2015 at 5:39pm
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace 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]++;
				} else if (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.
Last edited on Apr 10, 2015 at 5:43pm
Apr 10, 2015 at 5:50pm
You can't return an array from a function.
You can't return a pointer or reference to a local variable.

I recommend using either std::tuple<int, int, int> or making your own data structure to encapsulate the array.
Apr 10, 2015 at 5:51pm
really I can't return an array from a function didn't know that. thank you
Apr 11, 2015 at 11:35am
LB wrote:
You can't return a pointer or reference to a local variable.

Why not?
It seems to work...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int& local()
{
    static int var(5);
    return var;
}

int main()
{
    int& loc = local();
    
    std::cout << loc << std::endl;
}
http://cpp.sh/7jxs

And i think it's a very good way if you want to make a Makro for singletons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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:
    virtual void 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();
}
http://cpp.sh/2c22
Last edited on Apr 11, 2015 at 12:35pm
Apr 11, 2015 at 12:50pm
LB wrote:
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.
Apr 11, 2015 at 1:15pm
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

http://cpp.sh/4efhb
Apr 11, 2015 at 9:50pm
Last edited on Apr 11, 2015 at 9:56pm
Topic archived. No new replies allowed.