C++ library Linker Error

Hi

I am trying to write my own C++ string library. Well I have this standard C++ string library but I am practicing for my University Midterm Exam. Before this I use Dev-C++ and now I am kinda excited to use X-code on Mac OS. But every time I try to link this three files known as "Main.cpp" "Functions.cpp", where I have functions for my own string library and "string.h", which is my header file. But may be I can't find the right way to link and compile them. I got 141 errors. Yes they do work on Dev-c++ but not on this x-code.


strcmp is not a member of 'std'
memmove is not a member of 'std'
strxfrm has not been declared
strspn has not been declared
memchr has not been declared


These are strange error messages that I've never seen. May be I am new to X-code. Please show me some steps to compile in this kind of situation.


All of your answers will be appreciated
Thank you
Andrew Chang

Mac Book pro 15", Mac OS X (10.4.8), Core 2 duo 2.16GHz Ram-2GB

Posted on Jun 27, 2007 6:45 AM

Reply
7 replies

Jun 27, 2007 7:05 AM in response to MBP VS VAIO SZ

"Main.cpp"

#include "String.h"
#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;

unsigned int nPass = 0; // Keeps track of number of passed tests
unsigned int nFail = 0; // Keeps track of number of failed tests

void CTest(bool cond, const char * msg)
// function to increment the pass counter and fail counter.
// Also displays message showing what test failed when a failure occurs
{
if (cond)
++nPass;
else {
cout << "Test Failure: " << msg << endl;
++nFail;
}
}

int main()
{
try{

String cin_getline;
cout<<"Input through getline : ";
cin_getline.getline(cin);
cout<<"RESULT : "<<cin_getline<<endl<<endl;
//Testing getline(cin);


String fname;
CTest( fname.length() == 0, "Default Constructor and the length");
//Test default constructor


String gname = "Testing";
fname = gname = gname; // **FUNCTION CALLED HERE **
CTest( gname == "Testing" , "Operator= failed" );
CTest( fname == "Testing" , "Operator= failed" );
//Test for Assignment operator=


fname = "James"; // **FUNCTION CALLED HERE **
CTest( fname.length() == 5, "Assignment operator length checking failed");
CTest( fname == "James", "Assignment operator equality failed");
//Testing for assignment operator= , Testing for length, Equality


String lname("Bond"); // **FUNCTION CALLED HERE **
CTest( lname.length() == 4, "Constructor accepting C-string");
CTest( lname == "Bond", "Operator== and Implicit conversion");
//Test constructor accepting C-srting + Length + Equality


String fullname = fname + " " + lname + " " + lname;
// **FUNCTION CALLED HERE **
CTest(fullname == "James Bond Bond","operator+ and equality");
CTest(fullname.length() == 15,"operator+ and length");
//Test copy constructor and operator+ Length and Equality


fullname = " ";
fullname = fname =fullname+=fullname=lname;// **FUNCTION CALLED HERE **
CTest(fullname == "James Bond Bond","operator+ and equality");
CTest(fullname.length() == 15,"operator+ and length");
//Test for the operator+ function


String sname("computeradvance",16); // **FUNCTION CALLED HERE **
CTest(sname == "computeradvance","Copy Constructor with Size Failed");
CTest(sname.length() == 16,"Copy Constructor,operator+ and length");
//Test Copy Constructor with user's Size


String iname('x',5);
CTest( iname == "xxxxx", "Constructor with initialization char failed ");
CTest( iname.length() == 5, "Constructor with initialization wrong size ");
//Test constructor with initialization


String pname("AndyChang");
pname.resize(13,'*'); // **FUNCTION CALLED HERE **
CTest(pname == "AndyChang **","Copy Constructor with Size Failed");
CTest(pname.length() == 13," Resizing Failed ");
//Test Resizing Function


String sub = pname.substr(0,9);
CTest( sub == "AndyChang","C-Style substring Failed ");
CTest( sub.length() == 9, "C-Style substring Wrong Size");
//Test C-style string.substr(x,x) function

CTest( fname == fullname, "Assignment operator and the equality == ");
CTest( pname != lname, "Assignment operator and the equality != ");

CTest( pname < lname, "Assignment operator and the equality < ");
CTest( lname > pname, "Assignment operator and the equality > ");

CTest( fname <= fullname, "Assignment operator and the equality <=");
CTest( pname <= lname, "Assignment operator and the equality <=");

CTest( fname >= fullname, "Assignment operator and the equality >= ");
CTest( lname >= pname, "Assignment operator and the equality >= ");


cout <<endl<<endl;
cout << "Number of Pass Function : " << nPass << endl;
cout << "Number of Fail Function : " << nFail << endl;
cout <<endl;

}// Try

catch(bad_alloc &err){
cout<<"ATTENTION!! System is OUT OF MEMORY"<<endl;
}
catch(InvalidIndex &idex){
cout<<"INDEX LOGIC FAILED"<<endl;
}
catch(InvalidSize &sz){
cout<<"Size is INVALID"<<endl;
}

system("PAUSE");
return 0;

}

Jun 27, 2007 7:08 AM in response to MBP VS VAIO SZ

"Function.cpp"

#include "String.h"
using std::cout;
using std::endl;


String::String(const char *item)throw(bad_alloc){
strSize = strlen(item); //Detect the character size
bufferSize= strSize*2+1;//Enlarge the buffer size by twice the real size
buffer = new char[bufferSize];//creat new character with the enlarged size
strcpy(buffer,item);//copy the item back to already enlarged in size buffer
}

String::String(char item,size_t size)throw(bad_alloc){

strSize = size; // Change the string size into a new size
bufferSize = strSize*2+1; // Enlarging the old buffer size into twice
char *oldBuffer = buffer; // the size of string size
buffer = new char[bufferSize];
delete [] oldBuffer;
if(size == 0){ // if the size is zero it will be declared
strcpy(buffer,"\0"); // as an empty string object
}
else{
for(int loop=0;loop<=size;loop++){
if(loop==size){ // setting the last index as empty
buffer[loop] = '\0';
break;
}
buffer[loop] = item; // copy the desire character into
} // the buffer
}
}

String::String(const char *item , size_t size)throw(bad_alloc){

strSize = strlen(item); //initializing values
bufferSize = strSize*2+1;
char *oldBuffer = buffer;
buffer = new char[bufferSize];
delete [] oldBuffer;
strcpy(buffer,item);

if(size == 0){ //User input empty size
strSize = 0;
strcpy(buffer,"\0"); //Maintain the original bufferSize
}
else{
strncpy(buffer,item,size); //copy needed value to buffer
for(int loop=size;loop<bufferSize;loop++){
buffer[loop] = '\0'; //setting empty value to lefted spaces
}
strSize = size;
}
}

String::String(const String &rhs)throw(bad_alloc){
strSize = rhs.strSize; //copy the string size
bufferSize = rhs.bufferSize; //copy the buffer size
char *oldBuffer = buffer;
buffer = new char[bufferSize];// new character with present buffer size
delete [] oldBuffer;
strcpy(buffer,rhs.buffer); //copy to buffer
}

String & String::operator=(const String&rhs)throw(bad_alloc){
if(this == &rhs){ // check for s=s;
return *this; // and use the copy to call =opertor again
}
else{
strSize = rhs.length(); //detecting rhs's size
if(bufferSize<strSize){ // my buffer smaller than rhs
bufferSize = strSize*2+1;
char *oldBuffer = buffer;//pointing the future old buffer
buffer = new char[bufferSize];//create a new one
delete [] oldBuffer; //old one has been deleted
}
buffer = rhs.buffer;
return *this; //support for one = two = three
}
}

String::String operator+(const String &lhs, const String &rhs)throw(bad_alloc){
String temp(lhs); //create temporary string with a value of left h.side
temp += rhs; //add Right h.side value with Left h.side
return temp; //return the added value
}

String & String::operator+=(const String & rhs)throw(bad_alloc)
{
if (this == &rhs) // check for s+=s;
{ String myself(rhs); // then create a copy of myself
return *this += myself; // and use the copy to call += opertor again
// It's a recursive call.
}
size_t newLength = length() + rhs.length();
if (bufferSize < newLength + 1) // If not enough space
{
bufferSize = 2 * newLength + 1;
// we will allocate a lot more space hoping that next concatenation
// will not require de-allocation and re-allocation of memory
char *oldBuffer = buffer; // save the pointer to old buffer
buffer = new char[bufferSize]; // allocate new buffer
// what if new fails?
strcpy(buffer,oldBuffer); // copy back the old buffer
delete [ ] oldBuffer; // Return the old buffer to the heap
}
strcat(buffer,rhs.buffer); // Append rhs
strSize = newLength;

return *this;
}

ostream & operator<<(ostream &output, const String &s)throw()
{
output << s.c_str(); //convert to c-type string
return output;
}

istream & operator>>(istream & in, String & str)throw(bad_alloc)
{
char c;
str = ""; // reset the string to be an empty string. =operator called
in >> std::ws; // Skip the initial white spaces.
// This is the default behaviour for the >> operator
while (in.get(c)) { // Keep getting a character at a time until EOF or Error
if ( str.strSize >= str.bufferSize) { // No more space on the buffer?
char *oldBuffer = str.buffer; // save the pointer to old buffer
str.bufferSize=str.bufferSize * 2 + 15 ; // increase the buffer size
str.buffer = new char[str.bufferSize]; // allocate new bigger buffer
// what if new fails?
strcpy(str.buffer, oldBuffer); // copy the strings to the new buffer
delete [] oldBuffer; // return the space to the heap
}
if (isspace(c)) {
// if next character is a white space (Remember a new line is white space)
in.putback(c); // put the white space back to the istream
// WHY? That's the standard behaviour of operator>>
str.buffer[str.strSize]= '\0'; // Do not forget to append the null
return in; // stop getting input and return
}

str.buffer[str.strSize] = c; //Append the new character to the string
++str.strSize; // Increement the size since there is one more character added
} // end while // the while loop ends because the read failed due to EOF or error
} // end of the function

istream & String::getline(istream &input,char delim)throw(bad_alloc)
{
char c;
strSize = 0; //initial string size
input >> std::ws; // Skip the initial white spaces.
while(input.get(c))//&& (c != delim)) //while able to read char and char is not delim
{
if (strSize > bufferSize) //buffer is full
{
char *oldBuffer = buffer; //create a new buffer
bufferSize = strSize*2+1;//make 16 more char in case of getting a long input
buffer = new char[bufferSize];//create new buffer
strcpy(buffer,oldBuffer);//copy old content to new buffer
delete []oldBuffer; //delete old buffer
}
if(c == delim){ // testing if it's needed to terminate
buffer[strSize] = '\0'; // with a given sign
break;
}
buffer[strSize] = c;//assign the character of the buffer to the current index
strSize++; //increment size of string
}
return input;
}

String String::substr(size_t start, size_t amount)throw(InvalidIndex,InvalidSize,bad_alloc){

if(start >= strSize){
throw InvalidIndex(); // Index is invalid
}
if(amount > strSize-start){
throw InvalidSize(); // Size is invalid to copy
}

char temp[bufferSize]; // declaring new char for the substring
for(int loop = 0;loop<bufferSize;loop++){
if(loop>=amount){
temp[loop] = '\0'; // making empty to the rest of buffersize
}
else{
temp[loop] = buffer[loop+start]; //copy substring
}
}

return temp;
}

void String::resize(size_t newSize, char c)throw(bad_alloc,InvalidSize){

if(newSize == 0){ // if the new size is zero, it will set
strSize = 0; // empty to the string
strcpy(buffer,"\0"); //
}
else{
strSize = strlen(buffer); //detect the present buffersize
if( bufferSize<newSize ){ //if the buffer size is less
bufferSize = newSize*2+1; //than the needed new size,
char *oldBuffer = buffer; //this will create a new
buffer = new char[bufferSize];//Character with a new Size
strcpy(buffer,oldBuffer);
delete [] oldBuffer; //deleting old buffer
}
if( newSize < strSize ){ // according to the logic of
strSize = newSize; // the loop
}
for(int loop = strSize;loop<=newSize;loop++){
if(loop==newSize){ // assign empty to the cut
buffer[loop] = '\0'; // part
break;
}
buffer[loop] = c; // copying the rest till it
} // reach the size
strSize = newSize; // make sure of the string
} // size before it quits
}

String::~String()throw(){ //destructor is called
}

void String::clear()throw(bad_alloc){
strSize = 0; // resize the string size to zero
bufferSize =1; // resize the buffer size to zero
char *oldBuffer = buffer; // taking a pointer to a buffer
buffer = new char[bufferSize];// creating a new buffer with a size of 1
delete [] oldBuffer; // deleting a pointer of an old buffer
strcpy(buffer,"\0"); // initialize the buffer with empty char
}

char& String::operator[](size_t index)throw(InvalidIndex){
if(index>strSize){ //index larger than string size will be somewhere
throw InvalidIndex();//empty or to some value out of the unintialized
} //part
return buffer[index];//if the index exists, return a character at index
}

const char& String::operator[] (size_t index)const throw(InvalidIndex){
if(index>strSize){ //index larger than string size will be somewhere
throw InvalidIndex(); //empty or to some value out of the unintialized
} //part
return buffer[index]; //if the index exists, return a character at index
}

bool operator==(const String &lhs, const String &rhs)throw(){
if(strcmp(lhs.c str(),rhs.cstr()) == 0){
return true;
} //The result is 0 when comparing means the two objects are equal
else{
return false;
}
}

bool operator!=(const String &lhs, const String &rhs)throw(){
if(lhs == rhs){
return false;
} //The result is not 0 when the two objects are unequal
else{
return true;
}
}

bool operator<(const String &lhs, const String &rhs)throw(){
if(strcmp(lhs.c str(),rhs.cstr()) < 0){
return true;
} //Tthe result negative values = the first object < the second
else{
return false;
}
}

bool operator<=(const String &lhs, const String &rhs)throw(){
if(rhs > lhs){
return true;
} //The result of 0 means first object is equal to the second
else{ //and negative numbers mean first object is smaller than the second
return false;
}
}

bool operator>(const String &lhs, const String &rhs)throw(){
if(lhs < rhs){
return false;
} //the result positive values = the first object > the second
else{
return true;
}
}

bool operator>=(const String &lhs, const String &rhs)throw(){
if(lhs < rhs){
return false;
} //The result of 0 means first object is equal to the second
else{//and 1 and larger means first object is larger than the second
return true;
}
}

Jun 27, 2007 7:10 AM in response to MBP VS VAIO SZ

"String.h"


// Description: String class provides ADT for collection of characters. String
// class objects are first-level objects confirming the full assignment and
// copying operations. The class encapsulates an internal C-style (null terminated)
// string.
//
// CONSTRUCTORS
// String(const char * initstr = "");
// Accepts a C-style string to initialize its own string. Default arguement
// is an empty string.
// This constructor also works as an implicit conversion constructor from C-style
// to our own String class.
//
//
// String(const char * initstr, size_t num ofchar);
// Accepts a C-style string to initialize with and the number of characters
// to use from the given C-style string.
// If num ofchar is 0, then creates an empty String object
// If num ofchar is >= strlen(initstr), the every character in initstr is
// used to initialize the String object
//
// String(char ch, size_t size = 1);
// Creates a String object with "size" number of "ch" characters.
// Default number of character is 1.
// If size is 0, then an empty String object is created.
// This constructor also works as an implicit conversion constructor from
// char to our own String class
//
// String(const String &rhs);
// Copy Constructor, Fully copies the rhs object.
//
// DESTRUCTORS
// ~String(); deletes the internal buffer
//
// PUBLIC MEMBER FUNCTIONS:
//
// String& operator=(const String &rhs);
// Assignment operator function
// The internal buffer may be de-alloacted and re-allocated.
//
// String & operator+=(const String &rhs);
// Append the string object, rhs, to the called object.
// The internal buffer may be de-alloacted and re-allocated.
//
// bool empty() const; returns true if String size is 0, false otherwise
//
// void resize(size_t newSize, char ch = '\0');
// resize the String object to the given size
// if newSize is == current size, then no changes occur
// if newSize is < current size, String size is reduced but buffer remains the same
// if newSize is > current size, String size is enlarged and the space is initialized
// with the given character, which has the default value of NUL character.
// The internal buffer may be de-alloacted and re-allocated.
//
// char& operator[](size_t index);
// Mutating Index operator function, returns a character at the given index.
// Return value is a reference to the character as to be used as an lvalue
//
// const char& operator[] (size_t index) const;
// Accessor Index Operator function, Return value is a constant reference
// to prevent being used as an lvalue
//
// const char *c_str() const;
// Returns the C-style string. This member function is provided rather than
// a conversation function to prevent unwanted implicit conversions
//
// size_t length() const;
// Returns the current length of the string
//
// void clear();
// Set the String object to be empty.
//
//
// istream & getline(istream & in = cin, char delim = '\n');
// in is the istream object from which to get input. Default is cin
// delim is the deliminator character to indicate the end of input. Default
// is the new line character
// getline gets input characters including initial white spaces upto the the
// "delim" character and stores the whole thing to the string object.
// "delim" character is removed from the input stream but is NOT part of the
// the string
// We provide this member function as convenience menas to get strings which
// have spaces in them since the default behaviour of operator>> is to
// skip the initial white spaces and to process up to next white space.


// NON-MEMBER FUNCTIONS:
//
// ostream & operator<<(ostream & out, const String &str);
// out is an ostream object, str is a String object to be written to out
// outputs the given String object to the given ostream object
// returns the ostream object
//
// istream & operator>>(istream & in, String &str);
// in is an istream object to get inputs from, str is an String object
// will get inputs from the istream and stores the input into the str
// returns the in object. Beginning white spaces are skipped and inputs
// are accepted until EOF, error or a white space is entered.
//
// bool operator==(const String &lhs, const String & rhs);
// Returns true if String objects, lhs and rhs are lexically same.
// bool operator!=(const String &lhs, const String & rhs);
// Returns true if lhs is NOT lexically same to rhs
// bool operator<(const String &lhs, const String & rhs);
// Returns true if lhs is lexically less than rhs
// bool operator<=(const String &lhs, const String & rhs);
// Returns true if lhs is lexically less than or equal to rhs
// bool operator>(const String &lhs, const String & rhs);
// Returns true if lhs is lexically greater than rhs
// bool operator>=(const String &lhs, const String & rhs);
// Returns true if rhs is lexically greater than lhs
//
// String operator+(const String &s1, const String& s2);
// s1 is the left operand string, s2 is the right operand string
// The member operator function returns a new string which is a concatenation
// of s1 and s2.

#ifndef STRING_H
#define STRING_H

#include <iostream>
using std::ostream;
using std::istream;
using std::cin;
using std::bad_alloc;
#include <cstddef> // for size_t
#include <new>

class InvalidIndex{};
class InvalidSize{};
class String {

// Below operator functions cannot be member functions as the left operands
// are istream or ostream object as you already know. Now, do we have to
// make them friends? What are the pros and cons?
friend ostream & operator<<(ostream &, const String &)throw();
//ostream &operator<<(ostream &out,const String &str)
/*Responsibility : To print out on the screen
Pre-condition : The object is ready to be accessed and print
Input : none
Output : none
Post-Condition : Printed
Exception : none
*/
friend istream & operator>>(istream &, String &)throw(bad_alloc);
/*Responsibility : To support user desire value ex.cin>>
Pre-condition : none
Input : user's input
Output : none
Post-Condition : user's input is on the heap
Exception : throw bad_alloc if a new creation of character fails
*/

friend String operator+(const String &, const String &)throw(bad_alloc);
//Appending is.. existing a is combined with new object b (a+b)
//concatenating is.. existing a is combined with new object b to form c=a+b
//String
/*Responsibility : Combine the two string objects
Pre-condition : The two objects are initialized and ready to be touched
Input : string object
Output : new string object(return by value not by reference)
return value coz' does not have any object to copy
instead return by value (support a bc)
Post-Condition : New string object has been created with a value or empty
Exception : throw bad_alloc if new creation of character fails
*/



public:
String(const char * = "")throw(bad_alloc);
/*Responsibility : To create a new string object with a given value or empty
Pre-condition : none
Input : input string value. If not then dafault empty value has
been set
Output : none
Post-Condition : New string object has been created with a value or empty
Exception : New String object construction failed under
insuffcient memory
*/

String(const char *, size t)throw(badalloc);
/*Responsibility : To create a string object with a given size
Pre-condition : The item to be failed, the size and the object is ready
Input : desire size
Output : none
Post-Condition : String object has been resized with a given size
Exception : throw bad_alloc if new creation of String fails
*/

String(char, size_t = 1)throw(bad_alloc);
/*Responsibility : To create a string object with a given size filled by
a given character. If the user doesn't declare the size,
it will set the default value of 1
Pre-condition : the size, the character to be filled and an object is ready
Input : size and character to be filled
Output : none
Post-Condition : An object has been created with a given size of 1
*/

String(const String &)throw(bad_alloc);
/*Responsibility : To copy to the string object with a given value (C-Style)
Pre-condition : Two objects are ready to be copied
Input : Value to be copied
Output : none
Post-Condition : Given string value has been copied
Exception : throw bad_alloc if new creation of character fails
*/

~String()throw();
/*Responsibility : To Delete a constructed object
Pre-condition : three data members are properly initialized
Input : none
Output : none
Post-Condition : An object is completely deleted by a destructor
Exception : none
*/


String& operator=(const String &)throw(bad_alloc);
/*Responsibility : Copy a string value to an object
Pre-condition : A string value is ready to copy to another object
Input : String value
Output : none
Post-Condition : A string value has been copied to a given object
Exception : throw bad_alloc if new creation of String fails
*/

String& operator+=(const String &)throw(bad_alloc);
/*Responsibility : To combine both string objects values and put in one
Pre-condition : Two string objects are ready to be accessed
Input : another string object
Output : none (modified the existing objects)
Post-Condition : Both string objects has been combine
Exception : throw bad_alloc if new creation of character fails
*/

char& operator[](size_t)throw(InvalidIndex);
//str[3]='k';
/*Responsibility : An operator to find the character at the given size
Pre-condition : A string of sizeth character
Input : size
Output : A character and sizeth position has been returned
Post-Condition : none
Exception : throw invalid index if the index is larger than string size
*/

const char& operator[] (size_t) const throw(InvalidIndex);
//const char& String::operator[](size_t index) const;
/*Responsibility : if the index exists, return a character at index
Pre-condition : A string of sizeth character
Input : size
Output : A character and sizeth position has been returned
Post-Condition : none
Exception : throw invalid index if the index is larger than string size
*/

const char *c_str() const throw(); //c_str = convert to C-string
//const char* String::c_str()const
//why const char? we don't want the user to get inside and change
//though he pointers

/*Responsibility : Return the buffer value whenever the funccion is called
Pre-condition : three data members are properly initialized
Input : none
Output : buffer value has been returned
Post-Condition : none
Exception : none
*/

size_t length() const throw();
/*Responsibility : Detect the length of an object
Pre-condition : three data members are properly initialized
Input : none
Output : Length of the String
Post-Condition : none
Exception : none
*/

bool empty() const throw();
/*Responsibility : To check whether a string object is empty or not
Pre-condition : three data members are properly initialized
Input : none
Output : true (if this string is empty)
false(if this string is not empty)
Post-Condition : none
Exception : none
*/

void clear()throw(bad_alloc);
/*Responsibility : Sets the String object to be an empty string
Pre-condition : A string object is ready to be cleared
Input : none
Output : none
Post-Condition : A string object has been cleared
Exception : Throw exception if new character failed
*/

String substr(size_t, size t)throw(InvalidIndex,InvalidSize,badalloc);
/*Responsibility : Accept the starting point and the amount of char it wants
wants to copy containing the C-Style substring nature
Pre-condition :
Input : Starting point and amount of characters
Output : Desire part has been cut and returned
Post-Condition : none
Exception : Starting point index is invalid
Amount of characters is larger than the string size
New creation failed (failed to get the memory)
*/

void resize(size_t, char = '\0')throw(bad_alloc,InvalidSize);
//String::resize(size_t newSize, char filler - '\0');
/*Responsibility : Resize a string object with the new given size
if the new size is bigger, extra spaces will be filled with '\0'
which is user defined

Pre-condition : none
Input : none
Output : none
Post-Condition : String object has been resized
*/



istream & getline(istream & = cin, char = '\n')throw(bad_alloc);
//if the user doesn't set the value.. it will set automatically to delim
/*Responsibility : To get the whole line of input (including spaces)
Pre-condition : none
Input : input string value
Output : none
Post-Condition : User's input line has been taken
Exception : Throw bad_alloc if creation of new character failed
*/


private:
size_t strSize;
size_t bufferSize;
char *buffer;

};

// Below operator functions are not made member functions so that implicit
// conversion constructor is applied to the first operand of C-style string.
// But why aren't they declared to be friends? What are the pros and cons?

bool operator==(const String &, const String &)throw();
bool operator!=(const String &, const String &)throw();
bool operator<(const String &, const String &)throw();
bool operator<=(const String &, const String &)throw();
bool operator>(const String &, const String &)throw();
bool operator>=(const String &, const String &)throw();
//check lexographical and compare

// Below are definitions for inline functions

// Definition can be made outside class delcaration like this yet made
// inline by specifying inline keyword like below
// They are very simple functions thus good candidates for inline

// Returns the C-style string.
inline const char * String::c_str() const throw()
{ return buffer; }

// Returns the length of the current string object
inline size_t String::length() const throw()
{ return strSize; }

inline bool String::empty() const throw()
{ return strSize == 0; }

// Note that inline functions need to be defined in the header file since
// their definition needs to be seen at every call point.

#endif

Jun 27, 2007 7:32 AM in response to MBP VS VAIO SZ

Your Dev-C++ library sounds really old. Follow these instructions to get your code working on MacOS X or any other modern compiler.

You need to add the include files for these functions. Unfortunately, some of them are in "string.h", which may cause you some problems as you seem to have named your file the same. You should rename your string files to be "MyString.h" and "MyString.cpp". Then, put your string classes in something like a "My" namespace. That will prevent any conflicts with the std namespace.

At this point, you can add string.h include and everything should work. If you want to be really fancy, you can include <cstring> and at least some of those functions will be inside the "std" namespace.

Jun 27, 2007 10:05 AM in response to etresoft

Thank you so much for the help

Now I can compile already. I first created

Command line Utility/Standard Tool

I got main.cpp in the list. Then I right-clicked this "main.cpp" and then I add new file

BSD/C++ File

I got something.cpp and something.h two files. then I just copy and paste the contents from my original files from DEV-C++ that I can't compile.

Fortunately I can compile right now. And it shows the exact output as in DEV-C++. But I got these extra outputs in the compilation time.






xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
X-CODE OUTPUT
Input through getline : andy
RESULT : andy

andy(2086) malloc: * Deallocation of a pointer not malloced: 0x90025c22; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
andy(2086) malloc: * Deallocation of a pointer not malloced: 0x13564; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
andy(2086) malloc: * Deallocation of a pointer not malloced: 0x90025c22; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
andy(2086) malloc: * Deallocation of a pointer not malloced: 0x8230; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
andy(2086) malloc: * Deallocation of a pointer not malloced: 0xaaa9a8a7; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
andy(2086) malloc: * Deallocation of a pointer not malloced: 0xbffffa48; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
andy(2086) malloc: * Deallocation of a pointer not malloced: 0x3c; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug


Number of Pass Function : 27
Number of Fail Function : 0
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

--------------------------------------------------------
DEV-C++ OUTPUT

Input through getline : andy
RESULT : andy

Number of Pass Function : 27
Number of Fail Function : 0
--------------------------------------------------------

Is there anyway I can create empty project and start compiling ? Why the Build icon is disable in an empty project ?

Thank you...

Jun 27, 2007 5:41 PM in response to MBP VS VAIO SZ

Is there anyway I can create empty project and start
compiling ? Why the Build icon is disable in an empty
project ?


No. An empty project doesn't know what to do. That is why there are so many project templates. If you are porting something that already has a Makefile, just keep using make. If you really want to use XCode, create a new project using the template that is closest to what you want to do. For your project, the C++ Tool is probably best.

As for the messages, you have some memory problems. I will look through your code and see if I can find something that jumps out at me.

Jun 27, 2007 5:49 PM in response to MBP VS VAIO SZ

String::String(const char *item)throw(bad_alloc){

make sure item isn't null
e = strlen(item); //Detect the character size
bufferSize= strSize*2+1;//Enlarge the buffer size
by twice the real size
buffer = new char[bufferSize];//creat new
character with the enlarged size
strcpy(buffer,item);//copy the item back to already
enlarged in size buffer


String::String(char item,size_t
size)throw(bad_alloc){

strSize = size; // Change the
string size into a new size
bufferSize = strSize*2+1; // Enlarging the
old buffer size into twice
char *oldBuffer = buffer; // the size of
string size
buffer = new char[bufferSize];
delete [] oldBuffer;

oldBuffer is unallocated space. Put buffer(0) in the ctor initializer list.

That's all I found, but I also lost interest pretty quick. Check for null. Use initializer lists. Check that dtor. I don't think it should be empty.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

C++ library Linker Error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.