problem passing cstring, incompatible types

This is a discussion on problem passing cstring, incompatible types within the C++ Programming forums, part of the General Programming Boards category; I'm writing a singly linked list class and I have just started. I'm having problems with Code: (*startPointer).name = name; ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116

    problem passing cstring, incompatible types

    I'm writing a singly linked list class and I have just started. I'm having problems with
    Code:
    (*startPointer).name = name;
    Here is my code:

    main.cpp
    Code:
    #include <iostream>
    #include <cstring>
    #include "LinkedList.h"
    
    using namespace std;
    
    int main()
    {
        cout << "Enter your name:" << endl;
        char myName[20];
        cin.getline(myName, 20, '\n');
        return 0;
    }
    LinkedList.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cstring>
    #include "../include/LinkedList.h"
    
    //default constructor
    LinkedList::LinkedList() : startPointer(NULL), length(0)
    {
    
    }
    
    //destructor
    LinkedList::~LinkedList()
    {
    
    }
    
    bool LinkedList::createNode(char name[])
    {
        if(startPointer == NULL)
        {
            startPointer = new aNode;//aNode is a struct
            (*startPointer).name = name;
            std::cout << "Should be the same: " << (*startPointer).name <<std::endl;
        }
        
        return true;
    }
    LinkedList.h
    Code:
    #ifndef LINKEDLIST_H
    #define LINKEDLIST_H
    
    class LinkedList
    {
        public:
            LinkedList();
            ~LinkedList();
            void addToList();
            bool createNode(char name[]);
    
        private:
            unsigned int length;
            struct aNode
            {
                char name[20];
                aNode *next;
            };
            aNode *startPointer;
    
    };
    #endif // LINKEDLIST_H
    In constructor 'LinkedList::LinkedList()':
    warning: 'LinkedList::startPointer' will be initialized after
    warning: 'unsigned int LinkedList::length'
    warning: when initialized here
    In member function 'bool LinkedList::createNode(char*)':
    error: incompatible types in assignment of 'char*' to 'char [20]'
    warning: no return statement in function returning non-void

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    You cannot just assign the pointer like that because (*startPointer).name is an array, and you cannot assign to an array. You need to copy the string, e.g.,
    Code:
    strcpy(startPointer->name, name);
    Now, why are you not using std::string so you can concentrate on learning how to implement a linked list?
    Last edited by laserlight; 10-22-2010 at 01:26 PM. Reason: Wrong explanation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    I fixed this and now I'm getting a bunch of errors about the constructor, destructor and createNode(char name[]) having multiple definitions.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    First you need to answer laserlight's question.
    Then you need to post your update source code.
    Also, NULL should be nullptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by Elysia
    Also, NULL should be nullptr.
    Feel free to ignore this if it does not work for you, probably until sometime next year (hopefully) when the next version of the C++ standard is published.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    I'd say that unless you have very specific requirements, you should be using a compiler that supports it, eg Visual Studio 2010 Express or newest GCC.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    main
    Code:
    #include <iostream>
    #include <cstring>
    #include "src\LinkedList.cpp"
    
    using namespace std;
    
    int main()
    {
        cout << "Enter your name:" << endl;
        char myName[20];
        cin.getline(myName, 20, '\n');
        return 0;
    }
    LinkedList.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    //#include <cstring>
    #include <string.h>
    #include "..\include\LinkedList.h"
    
    //default constructor
    LinkedList::LinkedList() : startPointer(NULL), length(0)
    {
    
    }
    
    //destructor
    LinkedList::~LinkedList()
    {
    
    }
    
    bool LinkedList::createNode(char name[])
    {
        if(startPointer == NULL)
        {
            startPointer = new aNode;//aNode is a struct
            strcpy((*startPointer).name, name);//can't just assign with = because can't do arr1[]=arr2[]
            std::cout << "Should be the same: " << (*startPointer).name <<std::endl;
        }
    
        return true;
    }
    LinkedList.h
    Code:
    #ifndef LINKEDLIST_H
    #define LINKEDLIST_H
    
    class LinkedList
    {
        public:
            LinkedList();
            ~LinkedList();
            void addToList();
            bool createNode(char name[]);//creates a node at end of list
    
        private:
            unsigned int length;
            struct aNode
            {
                char name[20];
                aNode *next;
            };
            aNode *startPointer;
    
    };
    
    #endif // LINKEDLIST_H
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h||In constructor 'LinkedList::LinkedList()':|
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h|19|warning: 'LinkedList::startPointer' will be initialized after|
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h|13|warning: 'unsigned int LinkedList::length'|
    H:\code_blocks_workspace\ThisWillWork\src\LinkedLi st.cpp|8|warning: when initialized here|
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h||In constructor 'LinkedList::LinkedList()':|
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h|19|warning: 'LinkedList::startPointer' will be initialized after|
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h|13|warning: 'unsigned int LinkedList::length'|
    H:\code_blocks_workspace\ThisWillWork\src\LinkedLi st.cpp|8|warning: when initialized here|
    obj\Debug\main.o||In function `LinkedList':|
    H:\code_blocks_workspace\ThisWillWork\src\LinkedLi st.cpp|8|multiple definition of `LinkedList::LinkedList()'|
    obj\Debug\src\LinkedList.o:H:\code_blocks_workspac e\ThisWillWork\src\LinkedList.cpp|8|first defined here|
    obj\Debug\main.o||In function `LinkedList':|
    H:\code_blocks_workspace\ThisWillWork\src\LinkedLi st.cpp|8|multiple definition of `LinkedList::LinkedList()'|
    obj\Debug\src\LinkedList.o:H:\code_blocks_workspac e\ThisWillWork\src\LinkedList.cpp|8|first defined here|
    obj\Debug\main.o||In function `~LinkedList':|
    H:\code_blocks_workspace\ThisWillWork\src\LinkedLi st.cpp|14|multiple definition of `LinkedList::~LinkedList()'|
    obj\Debug\src\LinkedList.o:H:\code_blocks_workspac e\ThisWillWork\src\LinkedList.cpp|14|first defined here|
    obj\Debug\main.o||In function `~LinkedList':|
    H:\code_blocks_workspace\ThisWillWork\src\LinkedLi st.cpp|14|multiple definition of `LinkedList::~LinkedList()'|
    obj\Debug\src\LinkedList.o:H:\code_blocks_workspac e\ThisWillWork\src\LinkedList.cpp|14|first defined here|
    obj\Debug\main.o:H:\code_blocks_workspace\ThisWill Work\src\LinkedList.cpp|20|multiple definition of `LinkedList::createNode(char*)'|
    obj\Debug\src\LinkedList.o:H:\code_blocks_workspac e\ThisWillWork\src\LinkedList.cpp|20|first defined here|
    ||=== Build finished: 10 errors, 6 warnings ===|


    I'm more familiar with cstrings and when I tried to use std::string I got some errors, so I'll change that latter thanks.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Don't include .cpp files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Quote Originally Posted by Elysia View Post
    Don't include .cpp files.
    I've had a great deal of difficulty understanding this. How does this make sense when LinkedList.cpp includes LinkedList.h? Wouldn't including LinkedList.h leave out LinkedList.cpp since it doesn't include it (where as LinkedList.cpp includes LinkedList.h)?

    Why does it work like this? In otherwords, why is what I did wrong?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Each source file is compiled separately, then combined to form the program.
    It makes sense to have declarations, which are going to be used in several source files, to reside in headers. Then you can copy and paste the contents of those headers into your source files. That is what happens when you include a header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Got it. Just curious why my compiler gives me these warnigns:

    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h||In constructor 'LinkedList::LinkedList()':|
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h|19|warning: 'LinkedList::startPointer' will be initialized after|
    H:\code_blocks_workspace\ThisWillWork\src\..\inclu de\LinkedList.h|13|warning: 'unsigned int LinkedList::length'|
    H:\code_blocks_workspace\ThisWillWork\src\LinkedLi st.cpp|8|warning: when initialized here|
    ||=== Build finished: 0 errors, 3 warnings ===|

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Because in your class definition, you have (in order):
    unsigned int length;
    ANode* StartPointer;
    But in your initializer list, you initialize them in reverse order. You might think that the compiler will initialize the values in the order it seems them in the initializer list, but that is not correct.
    In fact, it will initialize them in the order they appear in the your class definition.
    That is why your compiler is warning you.

    Also, you should try substituting NULL with nullptr. If you have C++0x support, this should compile and be better than NULL.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Quote Originally Posted by Elysia View Post
    Because in your class definition, you have (in order):
    unsigned int length;
    ANode* StartPointer;
    But in your initializer list, you initialize them in reverse order. You might think that the compiler will initialize the values in the order it seems them in the initializer list, but that is not correct.
    In fact, it will initialize them in the order they appear in the your class definition.
    That is why your compiler is warning you.
    Got it.
    Quote Originally Posted by Elysia View Post
    Also, you should try substituting NULL with nullptr. If you have C++0x support, this should compile and be better than NULL.
    I was unable to get this to work.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    #include "..\include\LinkedList.h"
    
    LinkedList::LinkedList() : length(0), startPointer(nullptr)
    {
    
    }
    
    LinkedList::~LinkedList()
    {
    
    }
    
    bool LinkedList::createNode(char name[])
    {
        if(startPointer == nullptr)
        {
            startPointer = new aNode;        strcpy((*startPointer).name, name);[]
            std::cout << "Your name is: " << (*startPointer).name <<std::endl;
        }
    
        return true;
    }
    I'm using MinGW 4.5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-09-2009, 05:26 PM
  2. passing argument from incompatible pointer type
    By bgalin in forum C Programming
    Replies: 2
    Last Post: 12-06-2009, 06:14 AM
  3. Incompatible string assignment types
    By Tigers! in forum C Programming
    Replies: 9
    Last Post: 10-28-2009, 11:08 PM
  4. incompatible pointer types
    By quasigreat in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 12:30 AM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 05:12 PM

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