References Help

Hello,
I can't figure out why this is giving me such an issue. Hopefully someone can help, I'm sure it's something simple that I'm just forgetting about references. This is the Header file:
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
#pragma once

template <class C>
class iohandler
{
private:
	C mX, mY;
	enum Operation{
		MULTIPLY=4,
		DIVIDE=3,
		SUBTRACT=2,
		ADD=1,
		M=4,
		D=3,
		S=2,
		A=1,
		UNDEFINED = 0
	};
	C finalresult;
	C& result = finalresult;
public:
	iohandler();
	~iohandler(void);
	C getInput(){
		using namespace std;
		cout << "please enter what you want to calculate: ";
		cin >> mX >> Operation >> mY;
		return mX, Operation, mY;
	};
	void performOperation(C mX, Operation, C mY, C& result){
		if (Operation == 1){
			result = mX + mY;
		} else if (Operation == 2){
			result = mX-mY;
		} else if (Operation == 3) {
			result = mX/mY;
		} else if (Operation == 4) {
			result = mX*mY;
		} else {
			cout << "you have entered an invalid calculation, exiting...." << endl;
		}
	};
	void printResult(C x){
		cout << "Your Result is: " << x << endl;	
	};
	C calculate(){
		getInput();
		performOperation(mX,Operation,mY);
		printResult(result);
	};
};

This is the main CPP:
1
2
3
4
5
6
7
8
9
10
11
12
// ConsoleApplication6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iohandler.h"


int _tmain(int argc, _TCHAR* argv[])
{
	iohandler<double> Calc;
	return 0;
}

And this is what VS2012 code analyzer is spitting out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
1>  stdafx.cpp
1>  ConsoleApplication6.cpp
1>c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\iohandler.h(20): error C2864: 'iohandler<C>::result' : only static const integral data members can be initialized within a class
1>          c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\iohandler.h(51) : see reference to class template instantiation 'iohandler<C>' being compiled
1>c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\iohandler.h(20): error C2327: 'iohandler<C>::finalresult' : is not a type name, static, or enumerator
1>          with
1>          [
1>              C=double
1>          ]
1>          c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(10) : see reference to class template instantiation 'iohandler<C>' being compiled
1>          with
1>          [
1>              C=double
1>          ]
1>c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\iohandler.h(20): error C2065: 'finalresult' : undeclared identifier
1>c:\users\dev\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\iohandler.h(20): error C2864: 'iohandler<C>::result' : only static const integral data members can be initialized within a class
1>          with
1>          [
1>              C=double
1>          ]
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

your error wrote:
only static const integral data members can be initialized within a class


This error is exactly what it sounds like. This code:

C& result = finalresult;

isn't supported by your compiler (though I do think it is legal in C++11).

If you want to do this, you'll have to initialize result in the constructor's initializer list.


However... I wouldn't do that. 'result' here serves no purpose. What is the point of having a reference that refers to another variable in the class? Why not just use finalresult and get rid of result completely?
Last edited on
You're right I just read something similar on StackOverflow. Apparently MS hasn't implemented in VS yet, but it is one of the C++11 recommendations.

I'll try with just final result. I was trying to practice with variables a bit LOL
Topic archived. No new replies allowed.