也许这在某处得到了回答,而我只是不知道要搜索的单词。我试图了解我是否看到了错误或者我不理解减号运算符。头文件中的常量分配是否会导致编译器跳过“-”?
我的项目具有以下结构,包括生成的文件:
测试 ├── 源 │ ├── TipCoordinate.cpp │ └── TipCoordinate.hpp └── 单元测试 ├── main.cpp ├── main.o ├── 生成文件 ├── TipCoordinate.o ├── UnitTest.pro └── UnitTest.pro.user
我在类头中定义了一个常量,如下所示:
class TipCoordinate{
public:
TipCoordinate() {};
~TipCoordinate() {};
void Z(const float z);
private:
static const float sREFERENCE_SPHERE_RADIUS = 12;
};
我的类 cpp 文件中的以下代码将无法编译:
#include "TipCoordinate.hpp"
void TipCoordinate::Z(const float z){
float z_origin_ = -sREFERENCE_SPHERE_RADIUS;
}
我收到错误.../model/TipCoordinate.cpp:143: error: undefined reference to 'TipCoordinate::sREFERENCE_SPHERE_RADIUS'
但是,如果我只是将其更改如下,那么它非常高兴:
#include "TipCoordinate.hpp"
void TipCoordinate::Z(const float z){
float z_origin_ = -1*sREFERENCE_SPHERE_RADIUS;
}
或者,这也适用:
#include "TipCoordinate.hpp"
void TipCoordinate::Z(const float z){
float z_origin_ = 0-sREFERENCE_SPHERE_RADIUS;
}
我认为 Adam Rosenfield 的回答解决了我的问题,但在回应一些评论时,我已将上述摘录替换为重现问题的最小、完整示例。正如一些人猜测的那样,我正在使用 g++。版本是 g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2,优化器似乎受到影响,因为我使用的是“Qt Creator 2.1.0,基于 Qt 4.7.2(32 位),构建于2011 年 3 月 11 日”。这是我的项目文件:
QT -= gui
INCLUDEPATH += ../src
TEMPLATE = app
SOURCES += main.cpp \
../src/TipCoordinate.cpp
HEADERS += \
../src/TipCoordinate.hpp
我的主文件如下,但是将项目模板更改为 lib 并删除主文件会导致问题消失。
int main() { return 0; }
我怀疑 Qt Creator 很重要,因为除了更改模板之外,当我更改其他看似无关的因素时,问题会消失或再次出现。出于这个原因,我在下面包含了 Qt Creator 生成的 Makefile:
#############################################################################
# Makefile for building: UnitTest
# Generated by qmake (2.01a) (Qt 4.7.3) on: Sun Oct 14 15:05:23 2012
# Project: UnitTest.pro
# Template: app
# Command: /usr/local/Trolltech/QtEmbedded-4.7.3/bin/qmake -spec /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ CONFIG+=debug QMLJSDEBUGGER_PATH=/usr/share/qtcreator/qml/qmljsdebugger -o Makefile UnitTest.pro
#############################################################################
####### Compiler, tools and options
CC = gcc
CXX = g++
DEFINES = -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS = -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
INCPATH = -I/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ -I. -I/usr/local/Trolltech/QtEmbedded-4.7.3/include/QtCore -I/usr/local/Trolltech/QtEmbedded-4.7.3/include/QtNetwork -I/usr/local/Trolltech/QtEmbedded-4.7.3/include -I../src -I.
LINK = g++
LFLAGS = -Wl,-rpath,/usr/local/Trolltech/QtEmbedded-4.7.3/lib
LIBS = $(SUBLIBS) -L/usr/local/Trolltech/QtEmbedded-4.7.3/lib -lQtNetwork -L/usr/local/Trolltech/QtEmbedded-4.7.3/lib -lQtCore -lpthread
AR = ar cqs
RANLIB =
QMAKE = /usr/local/Trolltech/QtEmbedded-4.7.3/bin/qmake
TAR = tar -cf
COMPRESS = gzip -9f
COPY = cp -f
SED = sed
COPY_FILE = $(COPY)
COPY_DIR = $(COPY) -r
STRIP = strip
INSTALL_FILE = install -m 644 -p
INSTALL_DIR = $(COPY_DIR)
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE = rm -f
SYMLINK = ln -f -s
DEL_DIR = rmdir
MOVE = mv -f
CHK_DIR_EXISTS= test -d
MKDIR = mkdir -p
####### Output directory
OBJECTS_DIR = ./
####### Files
SOURCES = main.cpp \
../src/TipCoordinate.cpp
OBJECTS = main.o \
TipCoordinate.o
DIST = /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/g++.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/unix.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/linux.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/qws.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qconfig.pri \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/modules/qt_webkit_version.pri \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_functions.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_config.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/exclusive_builds.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_pre.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/debug.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_post.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/warn_on.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/unix/thread.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/moc.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/resources.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/uic.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/yacc.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/lex.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/include_source_dir.prf \
UnitTest.pro
QMAKE_TARGET = UnitTest
DESTDIR =
TARGET = UnitTest
first: all
####### Implicit rules
.SUFFIXES: .o .c .cpp .cc .cxx .C
.cpp.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.cc.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.cxx.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.C.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.c.o:
$(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"
####### Build rules
all: Makefile $(TARGET)
$(TARGET): $(OBJECTS)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
Makefile: UnitTest.pro /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++/qmake.conf /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/g++.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/unix.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/linux.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/qws.conf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qconfig.pri \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/modules/qt_webkit_version.pri \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_functions.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_config.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/exclusive_builds.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_pre.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/debug.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_post.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/warn_on.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/unix/thread.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/moc.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/resources.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/uic.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/yacc.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/lex.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/include_source_dir.prf \
/usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtNetwork.prl \
/usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtCore.prl
$(QMAKE) -spec /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ CONFIG+=debug QMLJSDEBUGGER_PATH=/usr/share/qtcreator/qml/qmljsdebugger -o Makefile UnitTest.pro
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/g++.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/unix.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/linux.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/qws.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qconfig.pri:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/modules/qt_webkit_version.pri:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_functions.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_config.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/exclusive_builds.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_pre.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/debug.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_post.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/warn_on.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/unix/thread.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/moc.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/resources.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/uic.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/yacc.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/lex.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/include_source_dir.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtNetwork.prl:
/usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtCore.prl:
qmake: FORCE
@$(QMAKE) -spec /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ CONFIG+=debug QMLJSDEBUGGER_PATH=/usr/share/qtcreator/qml/qmljsdebugger -o Makefile UnitTest.pro
dist:
@$(CHK_DIR_EXISTS) .tmp/UnitTest1.0.0 || $(MKDIR) .tmp/UnitTest1.0.0
$(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/UnitTest1.0.0/ && $(COPY_FILE) --parents ../src/TipCoordinate.hpp .tmp/UnitTest1.0.0/ && $(COPY_FILE) --parents main.cpp ../src/TipCoordinate.cpp .tmp/UnitTest1.0.0/ && (cd `dirname .tmp/UnitTest1.0.0` && $(TAR) UnitTest1.0.0.tar UnitTest1.0.0 && $(COMPRESS) UnitTest1.0.0.tar) && $(MOVE) `dirname .tmp/UnitTest1.0.0`/UnitTest1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/UnitTest1.0.0
clean:compiler_clean
-$(DEL_FILE) $(OBJECTS)
-$(DEL_FILE) *~ core *.core
####### Sub-libraries
distclean: clean
-$(DEL_FILE) $(TARGET)
-$(DEL_FILE) Makefile
check: first
mocclean: compiler_moc_header_clean compiler_moc_source_clean
mocables: compiler_moc_header_make_all compiler_moc_source_make_all
compiler_moc_header_make_all:
compiler_moc_header_clean:
compiler_rcc_make_all:
compiler_rcc_clean:
compiler_image_collection_make_all: qmake_image_collection.cpp
compiler_image_collection_clean:
-$(DEL_FILE) qmake_image_collection.cpp
compiler_moc_source_make_all:
compiler_moc_source_clean:
compiler_uic_make_all:
compiler_uic_clean:
compiler_yacc_decl_make_all:
compiler_yacc_decl_clean:
compiler_yacc_impl_make_all:
compiler_yacc_impl_clean:
compiler_lex_make_all:
compiler_lex_clean:
compiler_clean:
####### Compile
main.o: main.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp
TipCoordinate.o: ../src/TipCoordinate.cpp ../src/TipCoordinate.hpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o TipCoordinate.o ../src/TipCoordinate.cpp
####### Install
install: FORCE
uninstall: FORCE
FORCE:
我看到出现了一个滚动框,但如果有办法让它看起来更简洁,我欢迎任何提示。