Results 1 to 11 of 11

Thread: char pointer cannot convert from 'const char [17]' to 'char'?

  1. #1

    char pointer cannot convert from 'const char [17]' to 'char'?

    The wonderful joys of wanting to throw your moniter out the window thanks to programming and the constant errors you get

    Ok but anyway I learnt to define a char pointer like this (from a book):
    Code:
    char * cpointer;
    cpointer = new char;
    *cpointer = "some sort of string";
    
    // and then its like an array of char
    
    if(cpointer[2] == 'm')
    {
        std::cout << "this will be displayed" << endl;    /* this will be displayed, because its true */
    }
    Im confused because the code above works, but when i put it into a class it kills itself
    (btw, dont ask me what im doing, i dont know, all i know is that i made some retarded class)
    Heres my class:
    Code:
    #include <iostream>
    #include "string.h"
    
    using namespace std;
    
    class car {
    private:
        int *year;
        int *gears;
        char *brand;
        char *model;
    public:
        // Constructor
        car(int yr = 2008, int grs = 6, string b = "no brand", string m = "no model");
        // Destructor
        ~car();
        
        int getYear();
        char *getModel();
        char *getBrand();
    };
    
    // class car Constructor
    car::car(int yr, int grs, string b, string m)
    {
    int i;
    bool bflag = true, mflag = true;
    
        year = new int;
        *year = yr;
        gears = new int;
        *gears = grs;
        brand = new char;
        *brand = "no brand        "; // <-- error: cannot convert from 'const char [17]' to 'char'
        model = new char;
        *model = "no model        "; // <-- error: cannot convert from 'const char [17]' to 'char'
    
    
    //  Ignore this big bunch of crap, I was fiddling around a bit (maybe i shoulnt)
    //  It basically makes the pointers hold the characters of the respective strings.
        for(i = 0; i <= 17; i++)
        {
            if(b[i] == '\0')
            {
                bflag = false;
            }
            if(m[i] == '\0')
            {
                mflag = false;
            }
            if(bflag)
            {
                brand[i] = b[i];
            }
            if(mflag)
            {
                model[i] = m[i];
            }
        }
    }
    
    // class car Destructor
    car::~car()
    {
        delete year;
        delete gears;
        delete brand;
        delete model;
    }
    
    int car::getYear()
    {
        return *year;
    }
    
    char *car::getModel()
    {
        return model;
    }
    
    char *car::getBrand()
    {
        return brand;
    }
    
    void main()
    {
        char * msg = "Hello World!";
        
        car ford(1982, 6, "Ford");
    
        cout << "This is a " << ford.getYear() << " " << ford.getBrand() << " "
               << ford.getModel() << "." << endl;
        
        cout << &msg << "\nholds the value\n" << msg << endl;
    }
    Am i just a f**king noob at C++? I mean i did learn this stuff from books..
    What am i doing wrong here?
    and let me know if my code gave you a heart attack from how unstable it must be.

    Any help will be much appreciated.

  2. #2
    1,627
    posts
    hugeExplosions = true;
    You can't copy a c-style string onto another. A char* is a pointer to a null terminated string (i.e. a pointer to the memory address of where the string starts - the null terminator \0 tells the computer where the string ends)

    You should use strcpy(dest, source);
    MS Paint FTW!


  3. #3
    Use std::string if you are using C++.

    #include <string>

    std::string foo = "foobar";

    woah what is this int * stuff. That's not what pointers are used for. Just do int foo; A 32 pointer pointing to an int that is a member variable is worthless. You are using double the memory and lose cache look-up speed.

    Burn that book.

    It's been a year since I've used C++, but meh here.

    Code:
    #include <iostream>
    #include <string>
    
    class Car 
    {
    private:
        int year;
        int gears;
        std::string brand;
        std::string model;
    public:
        // Constructor
        Car(int year = 2008, int gears = 6, std::string brand = "no brand", std::string model = "no model");
        // Destructor
        ~Car();
        
        int GetYear();
        std::string& GetModel();
        std::string& GetBrand();
    };
    
    //Constructor
    Car::Car(int year, int gears, std::string brand, std::string model)
    {
        this.year = year;
        this.gears = gears;
        this.brand = brand;
        this.model = model;
    }
    
    //Destructor
    Car::~Car(){ }
    
    int Car::GetYear()
    {
        return year;
    }
    
    std::string & Car::GetModel()
    {
        return model;
    }
    
    std::string & Car::GetBrand()
    {
        return brand;
    }
    
    void main()
    {
        const std::string msg = "Hello World!";
        
        Car ford(1982, 6, "Ford");
    
        std::cout << "This is a " << ford.GetYear() << " " << ford.GetBrand() << " "
               << ford.GetModel() << "." << endl;
        
        std::cout << &msg << "\nholds the value\n" << msg << std::endl;
    }
    Just so you know std::endl flushes the buffer. Research it. Doesn't really matter for small programs just something to know.

    Good luck.

  4. #4
    Omg cant believe this, that book was teaching me dodgy things.

    Of what I can see you didnt use pointers, but instead use the addresses.
    Thanks for that insightful piece of code, I see how i should be doing my classes now,
    without randomly putting weird pointers in it
    i shall burn that book.

    Thanks for the help Sirisian

    Sorry, just another quick Question to add.
    C++ doesnt worry about spaces in-between things correct?
    by that i mean (just using my code for example)
    Code:
    std::string&GetModel();
    std::string& GetModel();
    std::string &GetModel();
    std::string  &  GetModel();
    are all the exact same right?
    Last edited by Sp˙rL; July 2nd, 2008 at 11:12 PM.

  5. #5
    C++ added the address of operator which takes away some of the necessity for pointers. I generally used pointers most of the time seeing as they can be null and exploiting that ability is very powerful as you'll learn.

    Pick up Sams Teach Yourself C++ in 21 days. It explains everything you need to know basically.

    One of the big things you have to understand is the stack and heap. I'll give you a quick run-down. The stack is extremely fast at allocation. When you allocate variables inside of a functions scope they go on the stack. The heap is all the other memory your program can use.

    If you do say:
    int main()
    {
    Foo * o = new Foo();
    return 0;
    }

    That allocates a pointer foo on the stack and makes it point to a new instance that is allocated on the heap.
    this will explain it: http://www.youtube.com/watch?v=CObg3tbT2lg

  6. #6
    Thinking in C++ by Bruce <something> is an awesome book I'd recommend. It's free too.

    That address operator is really returning a reference. It's basically a safe pointer that you can't mess around too much with.

    Being the C programmer I am, pointers ftw!
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  7. #7
    Quote Originally Posted by Sp˙rL View Post
    Sorry, just another quick Question to add.
    C++ doesnt worry about spaces in-between things correct?
    by that i mean (just using my code for example)
    Code:
    std::string&GetModel();
    std::string& GetModel();
    std::string &GetModel();
    std::string  &  GetModel();
    are all the exact same right?
    In any language, there needs to be some method of identifying end of an element. Tokens are delimited by white-space characters and by other tokens, such as operators and punctuation, but the compiler ignores multiple white-space characters. Try to compile each of those to see what happens.

  8. #8
    I only was able to get home and put that code (Sirisian's code) in my compiler today.
    It said there was some kind of problem with the code you had in the Constructor
    Code:
    //Constructor
    Car::Car(int year, int gears, std::string brand, std::string model)
    {
        this.year = year;
        this.gears = gears;
        this.brand = brand;
        this.model = model;
    }
    and it wouldnt work,
    I fixed it by using the pointer class member '->' thing

    Code:
    //Constructor
    Car::Car(int year, int gears, std::string brand, std::string model)
    {
        this->year = year;
        this->gears = gears;
        this->brand = brand;
        this->model = model;
    }
    Can some one explain why it needed that instead of just .memberName?

  9. #9
    1,627
    posts
    hugeExplosions = true;
    Because 'this' is a pointer - you can't use the dot . member access operator to access pointer members. You need to use the pointer -> member access operator!

    I think Sirisian is a bit rusty with the c

    'this' always points to the address of the calling object - hence it's a pointer!
    Last edited by Charleh; July 4th, 2008 at 05:58 AM.
    MS Paint FTW!


  10. #10
    Nah not rusty. I'm in the middle of programming C#. I knew fully well this was a pointer, just put the dot operator out of habit of using C# so frequently.

    Oh yeah and:
    std::string& foo = bar.GetModel();
    is the correct format. (Whitespace isn't important generally unless you deal with parser problem like:
    std::vector<std::vector<int> > where a space is required until C++0x fixes it so the template takes precedence to the >> operator.
    Last edited by Sirisian; July 4th, 2008 at 11:17 PM.

  11. #11
    You are absolute right




    ===========
    ccna

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012