我正在寫一個課程,我必須在那裏做一個紙牌遊戲,你必須匹配一副牌。它使用由教授提供的Qt編寫的GUI。我有一個包含名爲CardDeck的類的頭文件,它可以模擬一副牌。我將這個頭文件包含在主要的.cpp文件中,但每當我嘗試調用此類的任何成員函數時,編譯器都會打印出「CardDeck :: :(成員函數)'」的未定義引用。我有一個名爲CardDeck.cpp的.cpp文件,它定義了項目文件夾中的成員函數,但它可能無法正確編譯。我相當缺乏經驗,所以我不確定。任何幫助,將不勝感激。我在NetBeans IDE中使用MinGW編譯器。未定義的對象引用。可能的錯誤實現
main.cpp中:
#include "CardDeck.h"
using namespace std;
// Declare any global variables (your deck object) here:
CardDeck gdeck(9, 14, 2);
void shuffleCards(){
gdeck.ShuffleDeck();
}
CardDeck.h:
#ifndef CARDDECK_H
#define CARDDECK_H
#include "PlayingCard.h"
#include <vector>
class CardDeck {
public:
CardDeck();
CardDeck(int, int, int);
void ShuffleDeck();
void DisplayXCards(int);
void DisplayContents() const;
private:
std::vector<PlayingCard> deck;
int begin;
int end;
int number_each;
};
#endif /* CARDDECK_H */
CardDeck.cpp:
#include <vector>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include "PlayingCard.h"
#include "CardDeck.h"
using namespace std;
CardDeck::CardDeck(){
begin = 2;
end = 14;
}
CardDeck::CardDeck(int b, int e, int num_each){
begin = b;
end = e;
number_each = num_each;
char shapes[4] = {'H', 'S', 'C', 'D'};
for(int k=0; k<number_each; k++){
for(int j=0; j<4; j++){
for(int i = begin; i<= end; i++){
PlayingCard pc(i, shapes[j]);
deck.push_back(pc);
}
}
}
}
void CardDeck::DisplayContents() const{
for(int k=0; k<deck.size(); k++){
cout << deck[k].DisplayName() << endl;
}
}
void CardDeck::ShuffleDeck(){
random_shuffle(deck.begin(), deck.end());
}
void CardDeck::DisplayXCards(int n){
for(int i=0; i<n; i++){
cout << deck[i].DisplayName() << endl;
}
}
生成文件:
#############################################################################
# Makefile for building: cs215pgm3
# Generated by qmake (2.01a) (Qt 4.8.0) on: Thu Mar 1 15:03:59 2012
# Project: cs215pgm3.pro
# Template: app
# Command: c:/QtSDK/Desktop/Qt/4.8.0/mingw/bin/qmake.exe QMAKE_CC=C:/QtSDK/mingw/bin/gcc.exe QMAKE_CXX=C:/QtSDK/mingw/bin/g++.exe "QMAKE_CFLAGS=-g3 -gdwarf-2" "QMAKE_CXXFLAGS=-g3 -gdwarf-2" -o Makefile cs215pgm3.pro
#############################################################################
first: debug
install: debug-install
uninstall: debug-uninstall
MAKEFILE = Makefile
QMAKE = c:/QtSDK/Desktop/Qt/4.8.0/mingw/bin/qmake.exe
DEL_FILE = rm
CHK_DIR_EXISTS= test -d
MKDIR = mkdir -p
COPY = cp
COPY_FILE = $(COPY)
COPY_DIR = cp -r
INSTALL_FILE = $(COPY_FILE)
INSTALL_PROGRAM = $(COPY_FILE)
INSTALL_DIR = $(COPY_DIR)
DEL_FILE = rm
SYMLINK =
DEL_DIR = rmdir
MOVE = mv
CHK_DIR_EXISTS= test -d
MKDIR = mkdir -p
SUBTARGETS = \
debug \
release
debug: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug
debug-make_default: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug
debug-make_first: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug first
debug-all: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug all
debug-clean: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug clean
debug-distclean: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug distclean
debug-install: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug install
debug-uninstall: $(MAKEFILE).Debug FORCE
$(MAKE) -f $(MAKEFILE).Debug uninstall
release: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release
release-make_default: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release
release-make_first: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release first
release-all: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release all
release-clean: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release clean
release-distclean: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release distclean
release-install: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release install
release-uninstall: $(MAKEFILE).Release FORCE
$(MAKE) -f $(MAKEFILE).Release uninstall
Makefile: cs215pgm3.pro c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default/qmake.conf c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/qconfig.pri \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/modules/qt_webkit_version.pri \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/qt_functions.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/qt_config.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/exclusive_builds.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/default_pre.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/default_pre.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/debug.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/debug_and_release.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/default_post.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/default_post.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/rtti.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/exceptions.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/stl.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/shared.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/warn_on.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/qt.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/thread.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/moc.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/windows.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/resources.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/uic.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/yacc.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/lex.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/include_source_dir.prf \
c:/QtSDK/Desktop/Qt/4.8.0/mingw/lib/qtmaind.prl
$(QMAKE) QMAKE_CC=C:/QtSDK/mingw/bin/gcc.exe QMAKE_CXX=C:/QtSDK/mingw/bin/g++.exe "QMAKE_CFLAGS=-g3 -gdwarf-2" "QMAKE_CXXFLAGS=-g3 -gdwarf-2" -o Makefile cs215pgm3.pro
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/qconfig.pri:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/modules/qt_webkit_version.pri:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/qt_functions.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/qt_config.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/exclusive_builds.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/default_pre.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/default_pre.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/debug.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/debug_and_release.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/default_post.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/default_post.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/rtti.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/exceptions.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/stl.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/shared.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/warn_on.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/qt.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/thread.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/moc.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/win32/windows.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/resources.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/uic.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/yacc.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/lex.prf:
c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/features/include_source_dir.prf:
c:\QtSDK\Desktop\Qt\4.8.0\mingw\lib\qtmaind.prl:
qmake: qmake_all FORCE
@$(QMAKE) QMAKE_CC=C:/QtSDK/mingw/bin/gcc.exe QMAKE_CXX=C:/QtSDK/mingw/bin/g++.exe "QMAKE_CFLAGS=-g3 -gdwarf-2" "QMAKE_CXXFLAGS=-g3 -gdwarf-2" -o Makefile cs215pgm3.pro
qmake_all: FORCE
make_default: debug-make_default release-make_default FORCE
make_first: debug-make_first release-make_first FORCE
all: debug-all release-all FORCE
clean: debug-clean release-clean FORCE
distclean: debug-distclean release-distclean FORCE
-$(DEL_FILE) Makefile
check: first
debug-mocclean: $(MAKEFILE).Debug
$(MAKE) -f $(MAKEFILE).Debug mocclean
release-mocclean: $(MAKEFILE).Release
$(MAKE) -f $(MAKEFILE).Release mocclean
mocclean: debug-mocclean release-mocclean
debug-mocables: $(MAKEFILE).Debug
$(MAKE) -f $(MAKEFILE).Debug mocables
release-mocables: $(MAKEFILE).Release
$(MAKE) -f $(MAKEFILE).Release mocables
mocables: debug-mocables release-mocables
FORCE:
$(MAKEFILE).Debug: Makefile
$(MAKEFILE).Release: Makefile
編譯/錯誤信息:
/usr/bin/make -f Makefile.Debug
make[1]: Entering directory `/c/Users/Ben/Documents/CS 215/Programs/cs215Pgm3'
C:/QtSDK/mingw/bin/g++.exe -c -g3 -gdwarf-2 -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default' -o debug/cardwidget.o cardwidget.cpp
C:/QtSDK/mingw/bin/g++.exe -c -g3 -gdwarf-2 -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default' -o debug/cardwindow.o cardwindow.cpp
C:/QtSDK/mingw/bin/g++.exe -c -g3 -gdwarf-2 -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default' -o debug/main.o main.cpp
C:/QtSDK/Desktop/Qt/4.8.0/mingw/bin/moc.exe -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default' -D__GNUC__ -DWIN32 cardwindow.h -o debug/moc_cardwindow.cpp
C:/QtSDK/mingw/bin/g++.exe -c -g3 -gdwarf-2 -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default' -o debug/moc_cardwindow.o debug/moc_cardwindow.cpp
C:/QtSDK/Desktop/Qt/4.8.0/mingw/bin/moc.exe -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default' -D__GNUC__ -DWIN32 cardwidget.h -o debug/moc_cardwidget.cpp
C:/QtSDK/mingw/bin/g++.exe -c -g3 -gdwarf-2 -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include' -I'.' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/include/ActiveQt' -I'debug' -I'c:/QtSDK/Desktop/Qt/4.8.0/mingw/mkspecs/default' -o debug/moc_cardwidget.o debug/moc_cardwidget.cpp
g++ -mthreads -Wl,-subsystem,windows -o debug/cs215pgm3.exe debug/cardwidget.o debug/cardwindow.o debug/cs215pgm3.o debug/main.o debug/moc_cardwindow.o debug/moc_cardwidget.o debug/qrc_cards.o -L'c:/QtSDK/Desktop/Qt/4.8.0/mingw/lib' -lmingw32 -lqtmaind -lQtGuid4 -lQtCored4
make[1]: Leaving directory `/c/Users/Ben/Documents/CS 215/Programs/cs215Pgm3'
debug/cs215pgm3.o: In function `Z12shuffleCardsv':
C:\Users\Ben\Documents\CS 215\Programs\cs215Pgm3/cs215pgm3.cpp:107: undefined reference to `CardDeck::ShuffleDeck()'
debug/cs215pgm3.o: In function `_static_initialization_and_destruction_0':
C:\Users\Ben\Documents\CS 215\Programs\cs215Pgm3/cs215pgm3.cpp:99: undefined reference to `CardDeck::CardDeck(int, int, int)'
collect2: ld returned 1 exit status
make[1]: *** [debug/cs215pgm3.exe] Error 1
make: *** [debug] Error 2
BUILD FAILED (exit value 2, total time: 9s)
你是如何建立,製作文件,託管構建或其他方法?這聽起來像鏈接器不包括你的CardDeck.obj文件到版本中。如果您仔細閱讀編譯器輸出,您是否可以告訴它是編譯器錯誤還是鏈接器錯誤?換句話說,你能看到CardDeck編譯得乾淨的輸出嗎? – Tod 2012-03-03 21:01:24
它應該使用makefile進行構建。編譯器輸出的主要錯誤信息是「Build Failed:」,所以我假設編譯不正確 – doelling 2012-03-03 21:19:33
您能否向我們展示makefile? – Beta 2012-03-03 21:40:12