首页 > linux > linux-GCC编辑提示invalid initialization of non-const reference of type
2015四月14

linux-GCC编辑提示invalid initialization of non-const reference of type

自己写了个模块注册类,发现在window下正常,在GCC下编译就报上面的

invalid initialization of non-const reference of type

错误

查了下C++标准语法:

Initialization of a reference is trivial when the initializer is an lvalue (an object whose address you can take; see §4.9.6).
The initializer for a 'plain' T& must be an lvalue of type T. 
The initializer for a const T& need not be an lvalue or even of type T. 
In such cases (David''s notes: and only in such cases), [1] first, implicit type conversion to T is applied if necessary (see §C.6); [2] then, the resulting value is placed in a temporary variable of type T; and [3] finally, this temporary variable is used as the value of the initializer. 
Consider: 
    double& dr = 1;         // error: lvalue needed 
    const double& cdr = 1;  // ok 
The interpretation of this last initialization might be:
    double temp = double(1) ; // first create a temporary with the right value
    const double& cdr = temp; // then use the temporary as the initializer for cdr A temporary created to hold a reference initializer persists until the end of its reference’s scope. References to variables and references to constants are distinguished because the introduction of a temporary in the case of the variable is highly errorprone; an assignment to the variable would become an assignment to the – soon to disappear – temporary. No such problem exists for references to constants, and references to constants are often important as function arguments (§11.6).

解决方式:二选一 1.加上构造函数,默认构造函数不做任何初始化工作. 2.传参时加上const,这样才能启用系统默认的构造函数 代码如下:

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
class IDMReg;
typedef IDMReg* IDMRegPtr; 
class IDMReg
{
public:
	IDMReg(){}//1.
	virtual ~IDMReg(){}
};
class A
{
 
};
template<class T>
class  DMRegHelperT:public IDMReg
{
public:
	DMRegHelperT(){}
	~DMRegHelperT(){}
};
 
void Register(IDMReg&RegObj)//2.const也可加上
{
	int i=0;
}
 
int main(int argc, char *argv[])
{
	char sz[] = "Hello, World!";	//Hover mouse over "sz" while debugging to see its contents
	cout << sz << endl;	//<================= Put a breakpoint here
	Register(DMRegHelperT<A>());
	while (1)
	{
	}
	return 0;
}

文章作者:hgy413
本文地址:https://hgy413.com/2173.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

12 Responses to “linux-GCC编辑提示invalid initialization of non-const reference of type”

  1. #1 Cialis 回复 | 引用 Post:2019-02-19 18:51

    I do trust all of the ideas you have presented to your post.
    They’re really convincing and can definitely work. Nonetheless, the posts are very brief for novices.
    May you please prolong them a little from next time?
    Thanks for the post.

  2. #2 free xbox codes 回复 | 引用 Post:2020-06-07 04:13

    That is very attention-grabbing, You are an overly skilled blogger.
    I have joined your rss feed and sit up for searching for more of your excellent post.
    Also, I have shared your site in my social networks

  3. #3 g because 回复 | 引用 Post:2020-06-10 18:19

    I think that is among the such a lot significant info for
    me. And i am glad reading your article. But want
    to statement on some general issues, The web site style
    is perfect, the articles is in point of fact great : D. Excellent activity,
    cheers

  4. #4 g our 回复 | 引用 Post:2020-06-10 19:01

    Thanks for another informative blog. The place else may
    just I get that kind of information written in such a perfect approach?

    I’ve a undertaking that I am just now running on, and
    I’ve been at the look out for such info.

  5. #5 new g 回复 | 引用 Post:2020-06-11 23:53

    You have made some decent points there. I looked on the internet for more information about the issue and found most people will go along with your views on this website.

  6. #6 king of avalon hack 回复 | 引用 Post:2020-06-13 15:35

    I want to to thank you for this wonderful read!! I absolutely loved every little bit of it.
    I’ve got you book-marked to check out new stuff you post…

  7. #7 cheats for afk arena 回复 | 引用 Post:2020-06-14 02:22

    These are actually enormous ideas in regarding blogging.
    You have touched some fastidious factors here. Any way
    keep up wrinting.

  8. Great delivery. Sound arguments. Keep up the good effort.

  9. hi!,I love your writing so a lot! share we keep in touch more approximately your
    article on AOL? I require a specialist on this house to solve my
    problem. May be that is you! Looking forward to peer you.

  10. Hey! I could have sworn I’ve been to this blog before but after reading through some
    of the post I realized it’s new to me. Nonetheless, I’m definitely glad I found it and I’ll be bookmarking and checking back often!

  11. #11 http://tinyurl.com 回复 | 引用 Post:2020-06-14 13:15

    Saved as a favorite, I love your web site!

  12. #12 g 回复 | 引用 Post:2020-06-15 07:51

    Every weekend i used to go to see this site, for the reason that i want enjoyment, as this this site conations genuinely good funny material too.

发表评论