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):
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.
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)
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.
#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.
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 :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)
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
[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)
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
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!
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.