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 :expressionless:

Ok but anyway I learnt to define a char pointer like this (from a book):


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:



#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* == '\0')
        {
            bflag = false;
        }
        if(m* == '\0')
        {
            mflag = false;
        }
        if(bflag)
        {
            brand* = b*;
        }
        if(mflag)
        {
            model* = m*;
        }
    }
}

// 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 << "
holds the value
" << 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.

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);

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. :frowning:

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


#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 << "
holds the value
" << 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.

Omg cant believe this, that book was teaching me dodgy things. :m:

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 :thumb2:

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)


std::string&GetModel();
std::string& GetModel();
std::string &GetModel();
std::string  &  GetModel();

are all the exact same right?

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

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!

[QUOTE=SpĆærL;2352782]
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)


std::string&GetModel();
std::string& GetModel();
std::string &GetModel();
std::string  &  GetModel();

are all the exact same right?[/QUOTE]

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.

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


//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


//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?

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! :stuck_out_tongue:

I think Sirisian is a bit rusty with the c :smiley:

ā€˜this’ always points to the address of the calling object - hence it’s a pointer!

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. :stuck_out_tongue:

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.