Linker error and no classes appear
- From: "Brad Cooper" <Brad.Cooper_17@xxxxxxxxxxx>
- Date: Thu, 11 Aug 2005 06:11:50 GMT
Hi,
I have a question about visual C++ version 6. I am new to C++
I am trying to make work a program called Alphabet.cpp
from the book "Introduction to Computer Science" by
Lambert, Nance and Naps.
Chapter 7 refers to the files from the disk which
came with the book. Also at bottom of this posting.
To run the program Alphabet in VC++ I do the following...
1. File -> New -> Projects -> Win32 Console Application
2. Project Name: Alphabet
Location: d:\Brad
Platform: Win32
3. An Empty Project
Into the directory d:\Brad\Alphabet I copy the files which come with the
book:
Boolean.h
Strlib.h
Alphabet.cpp
Strlib.cpp
These files appear at the bottom of this email.
I then select Project -> Add to Project -> Files
and add the above four files to the project.
When I select Build -> Alphabet.exe
The programs compile with no errors, but they fail to link...
Linking...
LIBC.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Release/Alphabet.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Under the "ClassView", no classes appear. I think classes should appear
there - am I right?
I suspect I am making a basic error, but none of the VC++ books I have
explore such a simple setup. Any help much appreciated.
// Program file: alphabet.cpp
#include <iostream.h>
#include "boolean.h"
#include "strlib.h"
int main()
{ string word1, word2, temp;
cout << "Enter the first word: ";
cin >> word1;
cout << "Enter the second word: ";
cin >> word2;
if (word1 > word2)
{ temp = word1;
word1 = word2;
word2 = temp;
}
cout << word1 << " " << word2 << endl;
return 0;
}
// Library header file: boolean.h
#ifndef BOOL_H
#undef TRUE
#undef FALSE
const int TRUE = 1;
const int FALSE = 0;
typedef int boolean;
#define BOOL_H
#endif
// Class implementation file: strlib.cpp
// Implementation section
#include <string.h>
#include <assert.h>
#include "strlib.h"
string::string()
{
data[0] = '\0';
}
string::string(const char str[MAX_STRING_SIZE])
{
strcpy(data, str);
}
string::string(const string &str)
{
strcpy(data, str.data);
}
int string::length()
{
return strlen(data);
}
char string::nth_char(int n)
{
assert((n >= 1) && (n < MAX_STRING_SIZE));
return data[n - 1];
}
void string::append_char(char ch)
{
int length = strlen(data);
assert(strlen(data) < MAX_STRING_SIZE - 1);
data[length] = ch;
data[length + 1] = '\0';
}
boolean string::operator == (const string &str)
{
return strcmp(data, str.data) == 0;
}
boolean string::operator < (const string &str)
{
return strcmp(data, str.data) < 0;
}
boolean string::operator > (const string &str)
{
return strcmp(data, str.data) > 0;
}
boolean string::operator <= (const string &str)
{
return (strcmp(data, str.data) < 0) ||
(strcmp(data, str.data) == 0);
}
boolean string::operator >= (const string &str)
{
return (strcmp(data, str.data) > 0) ||
(strcmp(data, str.data) == 0);
}
boolean string::operator != (const string &str)
{
return strcmp(data, str.data) != 0;
}
string& string::operator = (const char str[MAX_STRING_SIZE])
{
strcpy(data, str);
return *this;
}
string& string::operator = (const string &str)
{
strcpy(data, str.data);
return *this;
}
istream& operator >> (istream &is, string &str)
{
is >> str.data;
return is;
}
ostream& operator << (ostream &os, const string &str)
{
os << str.data;
return os;
}
// Class definition file: strlib.h
#ifndef STRING_H
#include <iostream.h>
#include "boolean.h"
// Definition section
const MAX_STRING_SIZE = 21;
class string
{
public:
// Class constructor
string();
string(const char str[MAX_STRING_SIZE]);
string(const string &str);
// Function members
int length();
char nth_char(int n);
void append_char(char ch);
boolean operator == (const string &str);
boolean operator < (const string &str);
boolean operator > (const string &str);
boolean operator <= (const string &str);
boolean operator >= (const string &str);
boolean operator != (const string &str);
string& operator = (const char str[MAX_STRING_SIZE]);
string& operator = (const string &str);
friend istream& operator >> (istream &is, string &str);
friend ostream& operator << (ostream &os, const string &str);
protected:
// Data members
char data[MAX_STRING_SIZE];
};
#define STRING_H
#endif
.
- Prev by Date: Re: Operator new failing in windows after several days of operation
- Next by Date: Re: Operator new failing in windows after several days of operation
- Previous by thread: derived class functions, with dif arg lists
- Next by thread: problem with incremental linking
- Index(es):
Relevant Pages
|