本文介绍了是在定义的const值参数,但不是声明真的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于(但不同于)。



这里是一些简单的测试代码来说明我发现与Sun CC的奇怪:

  // --------------- main.cpp 
#includewtc.hpp

int main(int,char **)
{
testy t;
t.lame(99);
return 0;
}
//-------------wtc.hpp
#ifndef WTC_HPP_INCLUDED
#define WTC_HPP_INCLUDED

class testy
{
public:
void lame(int);
};

#endif

//---------------wtc.cpp
#include< iostream>
#includewtc.hpp

void testy :: lame(const int a)
{
std :: cout< 我被通过< a<< \\\
;
}

// --------------- makefile
#CXX = CC
CXX = g ++
#CXXFLAGS = -g
CXXFLAGS = -g3 -Wall -Werror

OBJECTS = $(patsubst%.cpp,%。o,$(wildcard * .cpp))

all:$(OBJECTS)
$(CXX)$(CXXFLAGS)-o $ @ $ ^

.PHONY:clean
clean:
rm * .o

当使用g ++编译时,它编译,链接,跑。
你也可以添加一个++ a;在testy :: lame()和编译器将抱怨更改只读变量(因为它应该)。



但是当我编译使用CC,以下链接程序错误:

  CC -g -c -o main.o main.cpp 
CC -g -c -o wtc.o wtc.cpp
CC -g -o all main.o wtc.o
未定义首先引用
文件中的符号
void testy :: lame(int) main.o
ld:fatal:引用符号引用错误。没有输出写入所有
make:*** [all]错误1

目标代码与nm和C ++ filt,我发现g ++版本创建一个
testy :: lame(int)符号,而CC创建testy :: lame(const int),因此链接器错误。 p>

我在Stroustrup的书里看了一下,但是找不到这个技术(不是说它不存在!所以这是真的一个编译器错误,或者只是一个黑客,其他地方工作,但Solaris?

解决方案

这看起来像是 CC 中的编译器问题。 C ++标准说明(在13.1可重载声明中):

但是有 const / volatile 修饰符可以参与重载,如标准稍后提到的: p>


This is similar to (but different from) this question.

Here is some simple test code to illustrate some weirdness I have discovered with Sun CC:

//---------------main.cpp
#include "wtc.hpp"

int main(int, char**)
{
  testy t;
  t.lame(99);
  return 0;
}
//--------------wtc.hpp
#ifndef WTC_HPP_INCLUDED
#define WTC_HPP_INCLUDED

class testy
{
public:
  void lame(int );
};

#endif 

//---------------wtc.cpp
#include <iostream>
#include "wtc.hpp"

void testy::lame(const int a)
{
  std::cout << "I was passed " << a << "\n";
}

//---------------makefile
#CXX=CC
CXX =g++
#CXXFLAGS= -g 
CXXFLAGS= -g3 -Wall -Werror

OBJECTS=$(patsubst %.cpp,%.o,$(wildcard *.cpp))

all : $(OBJECTS)
    $(CXX) $(CXXFLAGS) -o $@ $^

.PHONY: clean
clean :
    rm *.o

When this was compiled using g++ it compiles, links and does what you would expect when run.You can also add a ++a; in testy::lame() and the compiler will complain about changing a read-only variable (as it should).

However when I compile using CC, I get the following linker error:

CC -g   -c -o main.o main.cpp
CC -g   -c -o wtc.o wtc.cpp
CC -g -o all main.o wtc.o
Undefined                       first referenced
 symbol                             in file
void testy::lame(int)               main.o
ld: fatal: Symbol referencing errors. No output written to all
make: *** [all] Error 1

checking the object code with nm and C++filt, I find that the g++ version creates a testy::lame(int) symbol, whereas CC creates testy::lame(const int) , hence the linker error.

I looked it up in Stroustrup's book, but can't find this technique mentioned (doesn't mean it's not there!); so is this really a compiler bug, or just a hack that works everywhere else but Solaris?

解决方案

This looks like a compiler problem in CC. The C++ standard says (in 13.1 Overloadable declarations):

But there are const/volatile modifiers that can participate in overloading, as the standard mentions shortly afterwards:

这篇关于是在定义的const值参数,但不是声明真的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:32