Linker error?

This is a discussion on Linker error? within the C++ Programming forums, part of the General Programming Boards category; Ok. So I'm reading C++ Without Fear, and I have to say so far I'm enjoying the book. However, this ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    Linker error?

    Ok. So I'm reading C++ Without Fear, and I have to say so far I'm enjoying the book. However, this thread isn't for a book review. I've been using Dev-C++ to compile and run all the example programs in the book and so far it has worked perfectly. Until I got to this program:


    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main() {
        char str[600];
        char name[100];
        char addr[200];
        char work[200];
    
        // Get three strings from the user.
    
        cout << "Enter name and press ENTER: ";
        cin.getline(name, 99);
        cout << "Enter address and press ENTER: ";
        cin.getline(addr, 199);
        cout << "Enter workplace and press ENTER: ";
        cin.getline(work, 199);
    
        // Build the output string and then print it.
    
        strcopy(str, "\nMy name is ");
        strcat(str, name);
        strcat(str, ", I live at ");
        strcat(str, ",\nand I work at ");
        strcat(str, work);
        strcat(str, ".");
    
        cout << str;
    
        return 0;
    }
    The compiler doesn't seem to have a problem with this. The linker does however. It comes back with this message:

    g++: c:\documents and settings\owner\my documents\untitled1.o: No such file or directory
    g++: file path prefix `C:\DEV-C_~1\Bin\' never used


    Anyone have a clue as to what I'm doing wrong?

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    From what the linker is saying, an o file is a object file that keeps all the compiled binary data of a program. Perhaps it is having trouble placing it in the file. Try copying and pasting it to anoher file then compile it again. If you get the same error then check the DevC++ help files under linker. The only think I can think of is the object file is not being created. By By the way it should be

    Code:
    #include <string>
    not

    Code:
    #include <string.h>
    that is oid C++ syntax

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I have to say so far I'm enjoying the book
    Well, unfortunately the author doesn't understand C++ very well. cin.getline() is defined to read in a maximum number of characters that is equal to one character less than what is specified in the second parameter:

    cin.getline(name, 99);

    So, that line will only read in a maximum of 98 characters, and then it will tack on a '\0' character after the 98th character for a total of 99 characters. However, your array has a size of 100. The second parameter in cin.getline should be the size of your array.
    Last edited by 7stud; 03-14-2006 at 12:38 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    By the way it should be

    #include <string>

    not


    #include <string.h>
    Nope. The op is using c-style strings and c-string functions, so it should be:

    #include <cstring>

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Here's another error:

    strcopy(str, "\nMy name is ");

    There's no such function as strcopy().
    Last edited by 7stud; 03-14-2006 at 12:40 AM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    Quote Originally Posted by 7stud
    Well, unfortunately the author doesn't understand C++ very well. cin.getline() is defined to read in a maximum number of characters that is equal to one character less than what is specified in the second parameter:

    cin.getline(name, 99);

    So, that line will only read in a maximum of 98 characters, and then it will tack on a '\0' character after the 98th character for a total of 99 characters. However, your array has a size of 100. The second parameter in cin.getline should be the size of your array.
    Hrm. Yeah, I got all that. That's not affecting my ability to compile the program though. I also tried changing the preprocessor directive to #include <cstring> and it still gave me the same problems.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    Quote Originally Posted by 7stud
    Here's another error:

    strcopy(str, "\nMy name is ");

    There's no such function as strcopy().
    Yikes. You're right. That one was my error, though. Should have be strcpy(), right?

    EDIT: Alright, the program's running fine now. Thanks for the help!

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Just to add, that book you have seems to be pretty dataed, what year is it? I have one rather old called Practical C++ which used the old header types to. I used it when I had windows 95 - 98 and used Microsof Visual C++ 6.0 "shoot me.,,,," But thankfully I have upgraded everything since then. I guess the older the book is, the more error prone it will be as older compilers somtimes fail to understand newer functions and ideas

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    Quote Originally Posted by swgh
    Just to add, that book you have seems to be pretty dataed, what year is it? I have one rather old called Practical C++ which used the old header types to. I used it when I had windows 95 - 98 and used Microsof Visual C++ 6.0 "shoot me.,,,," But thankfully I have upgraded everything since then. I guess the older the book is, the more error prone it will be as older compilers somtimes fail to understand newer functions and ideas
    It was actually published in 2005. Go figure.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It was actually published in 2005
    Then the author is a hack (if that's your latest effort Bjarne, sorry!). Burn it and try something else.
    Last edited by 7stud; 03-14-2006 at 01:42 AM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Interestingly enough, that is the book the main page of this site recommends: http://www.cprogramming.com/books.html.

    I don't know if using 99 instead of 100 means the author is a hack, but I would recommend the books I recommended in the sticky at the top of this forum instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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