Won't compile straight from text!

This is a discussion on Won't compile straight from text! within the C++ Programming forums, part of the General Programming Boards category; Hi guys! Here's one that won't compile out of the box (student file from the text). Can anyone tell me ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Won't compile straight from text!

    Hi guys!

    Here's one that won't compile out of the box (student file from the text). Can anyone tell me why I'm getting:

    Data Structs error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    using namespace std;
    
    class Personal {
    public:
        Personal();
        Personal(char*,char*,char*,int,long);
        void writeToFile(fstream&) const;
        void readFromFile(fstream&);
        void readKey();
        int size() const {
            return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
        }
        bool operator==(const Personal& pr) const {
            return strcmp(pr.SSN,SSN) == 0;
        }
    protected:
        const int nameLen, cityLen;
        char SSN[10], *name, *city;
        int year;
        long salary;
        ostream& writeLegibly(ostream&);
        friend ostream& operator<<(ostream& out, Personal& pr) {
            return pr.writeLegibly(out);
        }
        istream& readFromConsole(istream&);
        friend istream& operator>>(istream& in, Personal& pr) {
            return pr.readFromConsole(in);
        }
    };
    
    Personal::Personal() : nameLen(10), cityLen(10) {
        name = new char[nameLen+1];
        city = new char[cityLen+1];
    }
    
    Personal::Personal(char ........n, char *n, char *c, int y, long s) :
            nameLen(10), cityLen(10) {
        name = new char[nameLen+1];
        city = new char[cityLen+1];
        strcpy(SSN,ssn);
        strcpy(name,n);
        strcpy(city,c);
        year = y;
        salary = s;
    }
    
    		void Personal::writeToFile(fstream& out) const {
        out.write(SSN,9);
        out.write(name,nameLen);
        out.write(city,cityLen);
        out.write(reinterpret_cast<const char*>(&year),sizeof(int));
        out.write(reinterpret_cast<const char*>(&salary),sizeof(int));
    }
    
    void Personal::readFromFile(fstream& in) {
        in.read(SSN,9);
        in.read(name,nameLen);
        in.read(city,cityLen);
        in.read(reinterpret_cast<char*>(&year),sizeof(int));
        in.read(reinterpret_cast<char*>(&salary),sizeof(int));
    }
    
    void Personal::readKey() {
        char s[80];
        cout << "Enter SSN: ";
        cin.getline(s,80);
        strncpy(SSN,s,9);
    }
    
    ostream& Personal::writeLegibly(ostream& out) {
        SSN[9] = name[nameLen] = city[cityLen] = '\0';
        out << "SSN = " << SSN << ", name = " << name
            << ", city = " << city << ", year = " << year
            << ", salary = " << salary;
        return out;
    }
    
    istream& Personal::readFromConsole(istream& in) {
        char s[80];
        cout << "SSN: ";
        in.getline(s,80);
        strncpy(SSN,s,9);
    		cout<<'['<<SSN<<']';
        cout << "Name: ";
        in.getline(s,80);
        strncpy(name,s,nameLen);
    		cout<<'['<<name<<']';
        cout << "City: ";
        in.getline(s,80);
        strncpy(city,s,cityLen);
    		cout<<'['<<city<<']';
        cout << "Birthyear: ";
        in >> year;
    		cout<<'['<<year<<']';
        cout << "Salary: ";
        in >> salary;
    		cout<<'['<<salary<<']';
    	cin.ignore();
        return in;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the class doesnt have an entry point (int main())

    create a 'driver' class that creates a variable of type Personal to test your 'Personal' class.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Technically, the code compiles. It just didn't link because you cannot make a program without a place to start.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by Daved View Post
    Technically, the code compiles. It just didn't link because you cannot make a program without a place to start.
    Thanks, didn't know that that would give me this particular error message.

    -Patrick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'd like to load a text file's contents cleanly at compile time
    By CharlieLewaska in forum C Programming
    Replies: 3
    Last Post: 01-31-2006, 10:10 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21