Homework help, building a personal namespace. I have the namespace file coded and am trying to compile. From reading for the last 3 hours, it seems like I have a linker error, and I am not sure how to fix it.
Here is the content of dataChecks.cpp (my assignment specifically says to name it with .cpp extension.)
namespace dataChecks
{
bool isValidInt( string str )
{
//Returns true if str can be parsed into a valid int, false otherwise.
int start = 0;
int i;
bool valid = true; // assume a valid integer
bool sign = false; // assume no sign
//Check for an empty string
if (int(str.length()) == 0) valid = false;
//Check for a leading sign
if (str.at(0) == '-' || str.at(0) == '+')
{
sign = true;
start = 1; //Start checking for digits after the sign
}
// Check that there's at least one character after the sign
if (sign && int(str.length()) == 1) valid = false;
// Now check the string, which you know
// has at least one non-sign character
i = start;
while (valid && i < int(str.length()))
{
if (!isdigit(str.at(i))) valid = false; // found a non-digit character
i++; // move to next character
}
return valid;
}
int getanInt()
{
//Returns a valid integer entered from the keyboard.
bool isvalidInt(string); // function declaration (prototype)
bool notanint = true;
string svalue;
while (notanint)
{
try
{
cin >> svalue; //accept a string input
if (!isvalidInt(svalue)) throw svalue;
}
catch (string e)
{
cout << "Invalid integer - Please reenter: ";
continue; //send control to while statement
}
notanint = false;
}
return atoi(svalue.c_str()); // convert to an integer
}
bool isValidReal( string str )
{
//Returns true if str can be parsed into a valid double, false otherwise.
int start = 0;
int i;
bool valid = true; // assume a valid integer
bool sign = false; // assume no sign
//Check for an empty string
if (int(str.length()) == 0) valid = false;
//Check for a leading sign
if (str.at(0) == '-' || str.at(0) == '+')
{
sign = true;
start = 1; //Start checking for digits after the sign
}
// Check that there's at least one character after the sign
if (sign && int(str.length()) == 1) valid = false;
// Now check the string, which you know
// has at least one non-sign character
i = start;
while (valid && i < int(str.length()))
{
if (!isdigit(str.at(i)) && str.at(i) == '.') valid = true; // found a non-digit character
i++; // move to next character
}
return valid;
}
double getaReal()
{
//Returns a valid real (double) entered from the keyboard.
bool isvalidReal(string); // function declaration (prototype)
bool notadouble = true;
string svalue;
while (notadouble)
{
try
{
cin >> svalue; //accept a string input
if (!isvalidReal(svalue)) throw svalue;
}
catch (string e)
{
cout << "Invalid Double - Please reenter: ";
continue; //send control to while statement
}
notadouble = false;
}
return atof(svalue.c_str()); // convert to an double
}
} // end of dataChecks namespace
//Project : Assignment 13
//File : Assignment 13.cpp
//Name : Kirby Wood
//Date : 04/03/14
//
// Description : Write a program to simulate managing test scores.
#include <iostream>
#include <string>
usingnamespace std;
#include </Volumes/Banksy/Users/kirbatron/HDD-Documents/C++/dataChecks.cpp>
usingnamespace dataChecks;
//Function prototypes
int main()
{
int value;
double doublevalue;
cout << "Enter an integer value: ";
value = getanInt();
cout << "Enter a double value: ";
doublevalue = getaReal();
return 0;
}
//function declarations and definitions
here is the error I get:
1 2 3 4 5 6 7 8
Undefined symbols for architecture x86_64:
"dataChecks::isvalidInt(std::string)", referenced from:
dataChecks::getanInt() in main.o
"dataChecks::isvalidReal(std::string)", referenced from:
dataChecks::getaReal() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Assignment is due by midnight, any help and explanation would be a lifesaver. Am compiling on Xcode 5.1. Thanks.
Compile each *.cpp to separate object file. Linker combines the objects. Do not include *.cpp into another *.cpp. Put declarations into a header file and include that where needed.