也許這是回答那裏的地方,我只是不知道要搜索它的話。我試圖瞭解我是否看到錯誤,或者如果我不瞭解減號運算符。在標題中分配常量會導致編譯器跳過' - '?未定義的引用 - 應該在C++中的`a = -b;`和`a = -1 * b;`和`a = 0-b'之間有區別嗎?
我的項目具有以下結構,包括生成的文件:
test ├── src │ ├── TipCoordinate.cpp │ └── TipCoordinate.hpp └── UnitTest ├── main.cpp ├── main.o ├── Makefile ├── 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;
}
我認爲亞當羅森菲爾德的回答解決了我的問題,但作爲對一些評論的迴應,我用一個重現問題的最小,完整的例子取代了上述摘錄。正如一些推測,我使用g ++。版本是g ++(Ubuntu/Linaro 4.5.2-8ubuntu4)4.5.2,並且優化器似乎可能會受到影響,因爲我使用的是「基於Qt 4.7.2(32位)的Qt Creator 2.1.0」,構建於2011年3月11日「。這裏是我的項目文件:
QT -= gui
INCLUDEPATH += ../src
TEMPLATE = app
SOURCES += main.cpp \
../src/TipCoordinate.cpp
HEADERS += \
../src/TipCoordinate.hpp
我的主要文件只是以下,但改變的項目模板庫和消除主文件導致該問題消失。
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 "[email protected]" "$<"
.cc.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "[email protected]" "$<"
.cxx.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "[email protected]" "$<"
.C.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "[email protected]" "$<"
.c.o:
$(CC) -c $(CFLAGS) $(INCPATH) -o "[email protected]" "$<"
####### 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:
我看到一個滾動框出現,但如果有一種方法,使顯示更簡潔,我歡迎任何提示。
這是什麼編譯器?聽起來像一個錯誤。 –
當我解決UVa上的編程問題時,我面臨同樣的問題。我在使用'abs'函數時遇到了錯誤的答案,但是當創建我自己的'abs'時,它已被接受! – 2012-10-12 20:51:12
我最好的猜測是,你沒有向我們展示一些小的變化。你能向我們展示一個展示錯誤的簡短自包含程序嗎? http://sscce.org/ –