5 Replies - 1873 Views - Last Post: 30 May 2010 - 10:41 PM Rate Topic: -----

#1 phasoninfused92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-May 10

Linker error while compiling

Posted 30 May 2010 - 03:45 PM

Hi. I keep getting an error while trying to compile this program.

The error says:

Quote

[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
C:\...\Makefile.win [Build Error] [OOSTRING.exe] Error 1


Here is the program (OOSTRING.CPP):
#include <string>
#include <cassert>
#include "oostring.h"
using namespace::std;

const int MAX_LENGTH = 1024;        // largest size string for input

oostring::oostring()
{
    Capacity = 1;
    CString = new char[Capacity];
    CString[0] = '\0';           // make c-style string zero length
}


oostring::oostring(const char * s)
{
    Capacity = strlen(s) + 1;
    CString = new char[Capacity];
    strcpy(CString,s);
}

oostring::oostring(const oostring & str)
{
    Capacity = str.length() + 1;
    CString = new char[Capacity];
    strcpy(CString,str.CString);
}

oostring::~oostring()
{
    delete [] CString;                // free memory
}

const oostring & oostring::operator = (const oostring & rhs)
{
    if (this != &rhs)                             // check aliasing
    {
	if (Capacity < rhs.length() + 1)        // more memory needed?
	{
	    delete[] CString;                   // delete old string
	    Capacity = rhs.length() + 1;        // add 1 for '\0'
	    CString = new char[Capacity];
	}
	strcpy(CString,rhs.CString);
    }
    return *this;
}


const oostring & oostring::operator = (const char * s)
{

    int len = 0;                         // length of newly constructed string
    assert(s != 0);                      // make sure s non-NULL
    len = strlen(s);                     // # of characters in string

    // free old string if necessary

    if (Capacity < len + 1)
    {
	delete[] CString;	// delete old string
	Capacity = len + 1;	// add 1 for '\0'
	CString = new char[Capacity];
    }
    strcpy(CString,s);
    return *this;
}

const oostring & oostring::operator = (char ch)
{
    if (Capacity < 2)
    {
	delete [] CString;
	Capacity = 2;
	CString = new char[Capacity];
    }
    CString[0] = ch;	// make string one character long
    CString[1] = '\0';
    return *this;
}

int oostring::length( ) const
{
  int myLength = 0;

  while(CString[myLength] != '\0')
    myLength++;

  return(myLength);
}

char * oostring::c_str() const
{
    return CString;
}

char & oostring::operator [] (int k)
{
    if (k < 0 || strlen(CString) <= k)
    {
	cerr << "index out of range: " << k << " string: " << CString
	    << endl;
	assert(0 <= k && k < strlen(CString));
    }
    return CString[k];
}

ostream & operator << (ostream & os, const oostring & str)
{
    return os << str.c_str();
}

istream & operator >> (istream & is, oostring & str)
{
    char buf[MAX_LENGTH];
    is >> buf;
    is.ignore(sizeof(buf),'\n');
    str = buf;
    return is;
}


istream & getline(istream & is, oostring & str)
{
    char buf[MAX_LENGTH];
    is.get(buf,sizeof(buf));
    is.ignore(sizeof(buf),'\n');
    str = buf;
    return is;
}

const oostring & oostring::operator += (const oostring & str)
{

    oostring copystring(str);         // copy to avoid aliasing problems

    int newLength = length() + str.length(); // self + added string
    int lastLocation = length();	     // index of '\0'

    // check to see if local buffer not big enough
    if (newLength >= Capacity)
    {
	Capacity = newLength + 1;
	char * newBuffer = new char[Capacity];
	strcpy(newBuffer,CString); // copy into new buffer
	delete [] CString;	     // delete old string
	CString = newBuffer;
    }

    // now catenate str (copystring) to end of CString
    strcpy(CString+lastLocation,copystring.c_str() );

    return *this;
}

const oostring & oostring::operator += (const char * s)
{

    int newLength = length() + strlen(s);    // self + added string
    int lastLocation = length();	     // index of '\0'

    // check to see if local buffer not big enough
    if (newLength >= Capacity)
    {
	Capacity = newLength + 1;
	char * newBuffer = new char[Capacity];
	strcpy(newBuffer,CString); // copy into new buffer
	delete [] CString;	     // delete old string
	CString = newBuffer;
    }

    // now catenate s to end of CString
    strcpy(CString+lastLocation,s);

    return *this;
}

const oostring & oostring::operator += ( char ch )
{
    oostring temp;	// make string equivalent of ch
    temp = ch;
    *this += temp;
    return *this;
}

oostring operator + (const oostring & lhs, const oostring & rhs)
{
    oostring result(lhs); // copies lhs to result
    result += rhs;	  // catenate rhs
    return result;	  // returns a copy of result
}

oostring operator + ( char ch, const oostring & str )
{
    oostring result;	  // make string equivalent of ch
    result = ch;
    result += str;
    return result;
}

oostring operator + ( const oostring & str, char ch )
{
    oostring result(str);
    result += ch;
    return result;
}


oostring oostring::substr(int pos, int len) const
{
    oostring result(*this);           // make sure enough space allocated

    if(pos < 0)                       // start at front when pos < 0
    {
      pos = 0;
    }

    if(pos >= strlen(CString))
    {
      result = "";       // empty string
      return result;
    }

    int lastIndex = pos + len - 1;      // last char's index (to copy)
    if(lastIndex >= strlen(CString))    // off end of string?
    {
      lastIndex = strlen(CString)-1;
    }

    int j,k;
    for(j=0,k=pos; k <= lastIndex; j++,k++)
    {
	result.CString[j] = CString[k];
    }
    result.CString[j] = '\0';         // properly terminate C-string
    return result;
}

int oostring::find(const oostring & str)  const
{
    int len = str.length();
    int lastIndex = length() - len;
    int k;
    for(k=0; k <= lastIndex; k++)
    {
	if (strncmp(CString + k,str.c_str(),len) == 0) return k;
    }
    return(-1);
}

int oostring::find( char ch ) const
{
    int k;
    for(k=0; k < strlen(CString); k++)
    {
	if (CString[k] == ch)
	{
	    return k;
	}
    }
    return(-1);
}


bool operator == ( const oostring & lhs, const oostring & rhs )
{
    return strcmp(lhs.c_str(), rhs.c_str()) == 0;
}

bool operator != ( const oostring & lhs, const oostring & rhs )
{
    return ! (lhs == rhs);
}

bool operator <  ( const oostring & lhs, const oostring & rhs )
{
    return strcmp(lhs.c_str(), rhs.c_str()) < 0;
}

bool operator <= ( const oostring & lhs, const oostring & rhs )
{
    return lhs < rhs || lhs == rhs;
}
bool operator >  ( const oostring & lhs, const oostring & rhs )
{
    return rhs < lhs;
}

bool operator >= ( const oostring & lhs, const oostring & rhs )
{
    return rhs <= lhs;
}

int oostring::converttoint() const
{
	return(atoi(CString));
}

double oostring::converttofloat() const
{
	return(atof(CString));
}



And here is the oostring.h header file:

#ifndef _OOSTRING_H
#define _OOSTRING_H

#include <iostream>
// uncomment line below if bool not built-in type
//#include "bool.h"
#include <cstdlib>
using namespace::std;

class oostring
{
  public:

  // constructors/destructor

    oostring( );                         // construct empty string ""
    oostring( const char * s );          // construct from string literal
    oostring( const oostring & str );    // copy constructor
    ~oostring( );                        // destructor


  // accessors

    int      length() const;                 // number of chars
    int      find(const oostring & str) const; // index of first occurrence of str
    int      find(char ch) const;            // index of first occurrence of ch
    oostring substr(int pos, int len) const; // substring of len chars
					     // starting at pos
    char * c_str( ) const;                   // explicit conversion to char *


  // assignment
    const oostring & operator = ( const oostring & str ); // assign str
    const oostring & operator = ( const char * s );       // assign s
    const oostring & operator = ( char ch );              // assign ch


  // indexing
    char & operator [] ( int k );             // range-checked indexing

  // modifiers
    const oostring & operator += ( const oostring & str ); // append str
    const oostring & operator += ( const char * s);        // append s
    const oostring & operator += ( char ch );              // append char

    int converttoint() const;
    double converttofloat() const;

  private:
      int   Capacity;                   // capacity of string
      char * CString;                   // storage for characters
};

// The following free (non-member) functions operate on strings
//
// I/O functions

ostream & operator << ( ostream & os, const oostring & str );
istream & operator >> ( istream & is, oostring & str );
istream & getline( istream & is, oostring & str );

// comparison operators:

bool operator == ( const oostring & lhs, const oostring & rhs );
bool operator != ( const oostring & lhs, const oostring & rhs );
bool operator <  ( const oostring & lhs, const oostring & rhs );
bool operator <= ( const oostring & lhs, const oostring & rhs );
bool operator >  ( const oostring & lhs, const oostring & rhs );
bool operator >= ( const oostring & lhs, const oostring & rhs );

// concatenation operator +

oostring operator + ( const oostring & lhs, const oostring & rhs );
oostring operator + ( char ch, const oostring & str );
oostring operator + ( const oostring & str, char ch );

#endif




I am using DevC++ v4.9.9.2
This is an example from a book I am using to learn C++. It is showing how to use a string class. I just don't know why it doesn't compile.

Is This A Good Question/Topic? 0
  • +

Replies To: Linker error while compiling

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Linker error while compiling

Posted 30 May 2010 - 03:48 PM

There's no main() function. Every program needs an entry point.
Was This Post Helpful? 0
  • +
  • -

#3 phasoninfused92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-May 10

Re: Linker error while compiling

Posted 30 May 2010 - 03:53 PM

Wow, I didn't even notice that. Would you have any idea what part is supposed to be in the main function? This is just a file from a work disc so I didn't write this.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Linker error while compiling

Posted 30 May 2010 - 03:58 PM

This looks like the intended main.
Was This Post Helpful? 0
  • +
  • -

#5 phasoninfused92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-May 10

Re: Linker error while compiling

Posted 30 May 2010 - 04:05 PM

I have that file as well, and when I try to compile it, I get this:

Quote

[Linker error] undefined reference to `oostring::oostring()'
[Linker error] undefined reference to `oostring::oostring(char const*)'
[Linker error] undefined reference to `oostring::oostring(oostring const*)'
[Linker error] undefined reference to `oostring::oostring(char const*)'
[Linker error] undefined reference to `oostring::length() const'
...
more undefined references to `oostring::~oostring()' follow
ld returned 1 exit status

Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Linker error while compiling

Posted 30 May 2010 - 10:41 PM

Try re-creating the project. Dev-CPP constantly fails at linking, with the undefined reference thing. I recommend you to stop using it, and get a newer Compiler, like Visual C++ 2010. It's free. And better.

This post has been edited by sarmanu: 30 May 2010 - 10:41 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1