ric@ace.sri.com (Richard Steinberger) (02/16/90)
I haven't programmed in c++ for a while, and so I thought I would try some-
thing "simple" as a refresher. I created 2 source files and a header file
that is included in 1 of the source files. Both compile, using GNU C++
on a VAX, but the linker detects some undefined symbols. The source
codes and header file follow, along with the error messages. Can anyone
explain what I've done wrong?
Please reply to: ric@ace.sri.com
Thanks to all who reply.
first source file:
// string.gxx
// Set up class of dynamically allocated strings.
#include <string.h>
#include <stream.h>
#define def_string_len 80
class string {
char *s;
int len;
public:
string(int n = def_string_len) {s = new char[n]; len = n;}
string(char *p) {len = strlen(p); s = new char[len + 1];
(void) strcpy(s,p);}
string(string&);
~string() {delete s;}
void assign(char *str) {(void) strcpy(s, str); len = strlen(str);}
void print() {cout << s << "\n";}
void concat(string&, string&);
};
string::string(string &str) //copy constructor
{
len = str.len;
s = new char[len + 1];
(void) strcpy(s, str.s);
}
void string::concat(string &a, string &b)
{
len = a.len + b.len;
s = new char[len + 1];
(void) strcpy(s,a.s);
(void) strcat(s,b.s);
}
second source file (main prog):
// test the string class.
#include "string_class.h"
main()
{
char *my_c_string = "Hello world.\n";
string a, b("Hi there.\n"), c("It's me.\n"), d, e(my_c_string);
a.assign("Is anyone home?\n");
d.concat(a,b);
d.print();
e.print();
}
header file:
//string_class.h - include file for string.gxx
#ifndef STRING_CLASS_H
#define STRING_CLASS_H
#define def_string_len 80
class string {
char *s;
int len;
public:
string(int = def_string_len);
string(char*);
string(string&);
~string();
void assign(char *);
void print();
void concat(string&, string&);
};
#endif
These are the VMS errors:
%LINK-W-NUDFSYMS, 5 undefined symbols:
%LINK-I-UDFSYM, ASSIGN__6STRINGPC
%LINK-I-UDFSYM, PRINT__6STRING
%LINK-I-UDFSYM, _$_STRING
%LINK-I-UDFSYM, __6STRINGI
%LINK-I-UDFSYM, __6STRINGPC