/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccjfOIT1.o:figure03_13.cpp:(.text+0x48): undefined reference to `GradeBook::GradeBook(std::string)'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccjfOIT1.o:figure03_13.cpp:(.text+0x48): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::GradeBook(std::string)'
> undefined reference to
Usually means one of three things.
1. You forgot to include GradeBook.cpp in your project.
2. You forgot to implement that specific function - specifically a GradeBook constructor with a string parameter.
3. You mis-spelled GradeBook as Gradebook or something similar.
I have gone through all the above-mentioned parameters but couldn't find the said discrepancies. Can there be a problem with compilation? As I am using notepad++ and taking the same route for compiling these files as used to take for simple files.
Apart from this, these files are containing the same names as the previous files because these files are the text examples from the book. I have copied the previous files into another folder but can those files create such errors?
NPP_EXEC: "C++ compile"
SET: G++ = C:\cygwin64\bin\g++.exe
$(G++) = C:\cygwin64\bin\g++.exe
NPP_SAVE: D:\C++\My C++ Program Collection\Chapter 3 Classes\3.11_3.12_3.13\figure03_13.cpp
CD: D:\C++\My C++ Program Collection\Chapter 3 Classes\3.11_3.12_3.13
Current directory: D:\C++\My C++ Program Collection\Chapter 3 Classes\3.11_3.12_3.13
"C:\cygwin64\bin\g++.exe" -Wall -Wextra -Wpedantic -std=c++11 -o "figure03_13" "figure03_13.cpp"
Process started (PID=11168) >>>
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x48): undefined reference to `GradeBook::GradeBook(std::string)'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x48): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::GradeBook(std::string)'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x96): undefined reference to `GradeBook::GradeBook(std::string)'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x96): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::GradeBook(std::string)'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0xd4): undefined reference to `GradeBook::getCourseName()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0xd4): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::getCourseName()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x105): undefined reference to `GradeBook::getCourseName()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x105): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::getCourseName()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x171): undefined reference to `GradeBook::setCourseName(std::string)'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x171): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::setCourseName(std::string)'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x1af): undefined reference to `GradeBook::getCourseName()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x1af): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::getCourseName()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x1e0): undefined reference to `GradeBook::getCourseName()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccmtDn6j.o:figure03_13.cpp:(.text+0x1e0): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GradeBook::getCourseName()'
collect2: error: ld returned 1 exit status
<<< Process finished (PID=11168). (Exit code 1)
================ READY ================
This command line contains only one source file, figure03_13.cpp. Is all your code in that one file? Is the function GradeBook::getCourseName() fully implemented in that file?
// Fig. 3.13: fig03_13.cpp
// Create and manipulate a GradeBook object; illustrate validation.
#include <iostream>
#include "GradeBookHeader.h" //include definition of class GradeBook
using namespace std;
//function main begins program execution
int main ()
{
// Create two GradeBook objects
// initial course name of gradeBook1 is too long
GradeBook gradeBook1( "CS101 Introduction to Programming in C++" );
GradeBook gradeBook2( "CS102 C++ Data Structures" );
// Display each GradeBook's courseName
cout << "gradebook1's initial course name is: "
<< gradeBook1.getCourseName()
<< "\n gradeBook2's initial Course name is: "
<< gradeBook2.getCourseName() << endl;
// modify myGradeBook's courseName (with a valid-length string)
gradeBook1.setCourseName( "CS101 C++ Programming" );
// display each GradeBook's courseName
cout << "\ngradeBook1's Course Name is: "
<< gradeBook1.getCourseName()
<< "\n gradeBook2's Course Name is: "
<< gradeBook2.getCourseName() << endl;
} //end main
2nd File : GradeBookCpp.cpp
// Fig. 3.12: GradeBook.cpp
// GradeBook member-function definitions. This file contains
// implementations of the member functions prototyped in GradeBook.h.
#include <iostream>
#include "GradeBookHeader.h"
using namespace std;
// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
setCourseName( name ); //call set fuction to initialize courseName
} // end GradeBook constructor
// function that sets the course name;
// ensures that the course name has at most 25 characters
void GradeBook::setCourseName( string name )
{
if ( name.length()<= 25 ) // if name has 25 or fewer characters
courseName = name; // store the course name in the object
if ( name.length() > 25 ) // if name has more than 25 characters
{
// set courseName to first 25 characters of parameter name
courseName = name.substr( 0,25 ); // start at 0, length of 25
cout << "Name \"" << name << "\" exceeds namimum length (25).\n"
<< "Limiting courseName to first 25 characters. \n" << endl;
} //end if
} // end function set course name
//function that gets the course name
string GradeBook::getCourseName()
{
return courseName;
} // end of function get course name
//function that displays a welcome message.
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << " Welcome to the Grade book for \n " << getCourseName()
<< "!" << endl;
} // ends function display message
3rd file: GradeBookHeader.h
// Fig. 3.11: GradeBook.h
// GradeBook class definition presents the public interface of
// the class. Member-function definitions appear in GradeBook.cpp.
#include <string> // program uses C++ standard string class
using namespace std;
//GradeBook class definition
class GradeBook
{
public:
GradeBook( string ); //Constructor that initializes courseName
void setCourseName( string ); //Function that sets the course name
string getCourseName(); //Function that gets the course name
void displayMessage(); //Function that displays a welcome message
private:
string courseName; // Course name for this GradeBook
}; // end class GradeBook
These are all separate files and saved in a single folder to keep them together and separate from the previous versions of these files.
You need your program to contain those Gradebook functions, so you have to compile that file as well. Your command line has to mention both cpp files. This will be easier if the three files are in the same directory.
Is this any different from compiling through notepad++... as to compile here... I just press the compile button... Do I need to try through the command line as well?
and whenever I compile
// Fig. 3.12: GradeBook.cpp
// GradeBook member-function definitions. This file contains
// implementations of the member functions prototyped in GradeBook.h.
#include <iostream>
#include "GradeBookHeader.h"
using namespace std;
// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
setCourseName( name ); //call set fuction to initialize courseName
} // end GradeBook constructor
// function that sets the course name;
// ensures that the course name has at most 25 characters
void GradeBook::setCourseName( string name )
{
if ( name.length()<= 25 ) // if name has 25 or fewer characters
courseName = name; // store the course name in the object
if ( name.length() > 25 ) // if name has more than 25 characters
{
// set courseName to first 25 characters of parameter name
courseName = name.substr( 0,25 ); // start at 0, length of 25
cout << "Name \"" << name << "\" exceeds namimum length (25).\n"
<< "Limiting courseName to first 25 characters. \n" << endl;
} //end if
} // end function set course name
//function that gets the course name
string GradeBook::getCourseName()
{
return courseName;
} // end of function get course name
//function that displays a welcome message.
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << " Welcome to the Grade book for \n " << getCourseName()
<< "!" << endl;
} // ends function display message
I get this error message:
NPP_EXEC: "C++ compile"
SET: G++ = C:\cygwin64\bin\g++.exe
$(G++) = C:\cygwin64\bin\g++.exe
NPP_SAVE: D:\C++\My C++ Program Collection\Chapter 3 Classes\3.11_3.12_3.13\GradeBookCpp.cpp
CD: D:\C++\My C++ Program Collection\Chapter 3 Classes\3.11_3.12_3.13
Current directory: D:\C++\My C++ Program Collection\Chapter 3 Classes\3.11_3.12_3.13
"C:\cygwin64\bin\g++.exe" -Wall -Wextra -Wpedantic -std=c++11 -o "GradeBookCpp" "GradeBookCpp.cpp"
Process started (PID=4684) >>>
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
/usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status
<<< Process finished (PID=4684). (Exit code 1)
================ READY ================
It seem LINK error not compile one, so has to do with declarations and/or definitions visibility/availability.. so watch the headers containing them, search with tool above
> Is this any different from compiling through notepad++... as to compile here... I just press the compile button...
From the look of things, notepad++ assumes that the current saved file is a complete program, and tries to compile it to an executable. As soon as you have a multi-file project, then notepad++ "Compile" is no longer adequate to the task.
> Do I need to try through the command line as well?
You should try that to start with, so you understand the process better.
Perhaps you should look at 'make' files, and then let notepad++ invoke the makefile to build all your source files.
unable to solve the upper mentioned issues... even tried with the different files (entirely new program) and with command line as well... but still found the same error again and even with command line.... too.....
So I am extremely stuck here ... need help in this regard as I can't move forward without solving this issue...
the same error even with the new file written from the scratch...
NPP_EXEC: "C++ compile"
SET: G++ = C:\cygwin64\bin\g++.exe
$(G++) = C:\cygwin64\bin\g++.exe
NPP_SAVE: C:\Users\FACULTY\Desktop\C++ Programs\Chapter 3 heart Exercise\HeartRateMain.cpp
CD: C:\Users\FACULTY\Desktop\C++ Programs\Chapter 3 heart Exercise
Current directory: C:\Users\FACULTY\Desktop\C++ Programs\Chapter 3 heart Exercise
"C:\cygwin64\bin\g++.exe" -Wall -Wextra -Wpedantic -std=c++11 -o "HeartRateMain" "HeartRateMain.cpp"
Process started (PID=6644) >>>
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0x1b): undefined reference to `HeartRates::HeartRates()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0x1b): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HeartRates::HeartRates()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0x80): undefined reference to `HeartRates::displayName(std::string)'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0x80): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HeartRates::displayName(std::string)'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xab): undefined reference to `HeartRates::enterCurrentDate()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xab): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HeartRates::enterCurrentDate()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xb7): undefined reference to `HeartRates::getDateOfBirth()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xb7): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HeartRates::getDateOfBirth()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xc3): undefined reference to `HeartRates::displayDateOfBirth()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xc3): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HeartRates::displayDateOfBirth()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xcf): undefined reference to `HeartRates::calculateAge()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xcf): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HeartRates::calculateAge()'
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xdb): undefined reference to `HeartRates::maxHeartRate()'
/cygdrive/c/Users/FACULTY/AppData/Local/Temp/ccJP1mP5.o:HeartRateMain.cpp:(.text+0xdb): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `HeartRates::maxHeartRate()'
collect2: error: ld returned 1 exit status
<<< Process finished (PID=6644). (Exit code 1)
================ READY ================
It all works fine as long as I have two files... The main and the class.... but it gives error once I have 3 files including 1. the main, 2. separate function and 3. the Class...
> "C:\cygwin64\bin\g++.exe" -Wall -Wextra -Wpedantic -std=c++11 -o "HeartRateMain" "HeartRateMain.cpp"
You're trying to build a complete program out of only part of the source code.
You need to make NP++ call a batch file containing