Jump to content
  • Advertisement
Sign in to follow this  
dcuk

what's wrong -working with pointers

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, my pointers seriously have me puzzled, when it outputs the numbers are like -5810019 and i have no idea why. Also prior to posting i made a few changes to the actual definition of my pointers, but doing compile errors now, im conviced that you can use Pointer 2, which has caused me the most grief, not sure if amateur error or compiler, so please help out, im just learning the trade and was progressing quite well. using the shell of hello world to breach out, was trying to go to linked lists afterwards.
#include <iostream>
#include <Windows.h>
#include <atldbcli.h>
#include <stdlib.h>




class DCMK
{
public: 
	DCMK() { int Aggression = 1; int Agility = 1; }
	DCMK(int tempA);
	~DCMK() {  std::cout << "Destructor Called <<\n"; }
	int GetAggression() { return(Aggression); }
	void SetAggression(int x) { int Aggression = x; }
	int GetAgility() { return(Agility); }
	void SetAgility(int x) { int Agility = x; }
private:
	int Aggression;
	int Agility;
};

DCMK::DCMK(int tempA) {
	int Aggression = tempA;
	int Agility = tempA;
	std::cout << "Constructor Called " << tempA << " \n";
}

class Data {
public:
	Data() { int pVal = rand();  int pStor = 1; } 
	Data(int val) { int pVal = val; }
	int GetValue(int wot); 
	int Beast(Data const &);
private:
	int pVal;
	int pStor;
};

Data::GetValue(int wot) {
	if(int wot = 1) { return pVal; }
	else { return wot; }
}

Data::Beast(Data const & other) {
	if(other.pStor < pStor)
		std::cout << "Worked" << this->pStor << "Sorted Geez!\n";
		std::cout << "Brup Brup!!!\n";
		return other.pStor;

}


int main()
{
	DCMK * pPoint = new DCMK; //pointer 1
	Data * D1; //pointer 2
	Data * D2(4) = new Data;  //pointer 3
	std::cout << "Hello, world!\n";
	std::cout << "Number is " << pPoint->GetAggression() << " for aggression\n";
	std::cout << "Number is " << pPoint->GetAgility() << " for aggression\n";
	std::cout << "Data is"  << D1.GetValue(1) << " Value\n";
std::cout << "Data is"  << D2.GetValue(1) << " Value\n";
	std::cout << "Data is"  << D2.Beast(2) << " Value\n";
	system ("pause");
	return 0;
};

Share this post


Link to post
Share on other sites
Advertisement
There are 2 things wrong with how you use pointers and one thing wrong with your code in general. First the pointers:

1. Data * D2(4) = new Data;
I'm thinking this would cause a compile error because it should actually be Data * D2 = new Data(4); You cannot pass in a value to the constructor of an object that does not yet exist, as you are doing.

2. std::cout << "Data is" << D1.GetValue(1) << " Value\n";
You cannot use the pointer D1. You have not created it on the heap with new yet. This is why you get weird numbers like -5810019.

Now in another place of your code I don't know if you noticed but in Data::GetValue you have an "if" statement that is actually assigning wot = 1 instead of comparing it to 1.

Share this post


Link to post
Share on other sites
thanks for the reply that really helped alot!, so thanks for that, im getting three error messages now, with the following sicne changing the "if statement" its triggered some errors, heres the code and erros at bottom, i don't understand it??


Data::GetValue(int wot) {
if(int wot == 1)
return pVal;
else
return wot;
}


Data::Beast(Data const & other) {
if(other.pStor < pStor)
std::cout << "Worked" << this->pStor << "Sorted Geez!\n";
std::cout << "Brup Brup!!!\n";
return other.pStor;
else
std::cout << "Buggered\n";
return other.pStor;
}





c:\Program Files\Microsoft Visual Studio\MyProjects\1st\1st.cpp(42) : error C2143: syntax error : missing ',' before '=='
c:\Program Files\Microsoft Visual Studio\MyProjects\1st\1st.cpp(44) : error C2181: illegal else without matching if
c:\Program Files\Microsoft Visual Studio\MyProjects\1st\1st.cpp(54) : error C2181: illegal else without matching if


Share this post


Link to post
Share on other sites
hmm, i'm pretty bad at reading others' code and making it work but here's what i think...


Data::GetValue(int wot) {
if(wot == 1)
return pVal;
else
return wot;
}


Data::Beast(Data const & other) {
if(other.pStor < pStor)
{
std::cout << "Worked" << this->pStor << "Sorted Geez!\n";
std::cout << "Brup Brup!!!\n";
return other.pStor;
}
else
{
std::cout << "Buggered\n";
return other.pStor;
}
}




basically what i think is that you need { } around your if/else statements that are longer than one line...

also you redefined wot as an int in ur if statement when it was already defined...

(not sure if that'll work... sorry if i made things worse)

Share this post


Link to post
Share on other sites

Data::GetValue(int wot) {
if(int wot == 1)



that is very bad.


Data::GetValue(int wot) {
if(wot == 1)



should be all u need to fix it:)

i think u can leave the {}'s out i know at least without an if statement u can so i dont' see a reason why not with both if and else's :)

Share this post


Link to post
Share on other sites
Quote:
Original post by -justin-

(not sure if that'll work... sorry if i made things worse)


Nope, that was correct advise :)

Another important point that has not been brought up before: you should try to learn more about the subject of scope. As pointed out by others there is a problem with your 'wot' variable. However, the problem occurs in many other places as well. Take for example your class definition:

class DCMK
{
public:
DCMK() { int Aggression = 1; int Agility = 1; }
DCMK(int tempA);
~DCMK() { std::cout << "Destructor Called <<\n"; }
int GetAggression() { return(Aggression); }
void SetAggression(int x) { int Aggression = x; }
int GetAgility() { return(Agility); }
void SetAgility(int x) { int Agility = x; }
private:
int Aggression;
int Agility;
};



All your functions redefine a variable named Agression or Agility. In your constructor

DCMK() { int Aggression = 1; int Agility = 1; }

you define two local variables int Aggression (sp? Agression?) and Agility and assign the value '1' to them. When that is done the constructor completes and throws away any local variables from the stack (Aggression and Agility) including their newly assigned values. The two class variables Aggression and Agility will be left untouched by the constructor and most likely contain garbage (or perhaps are both 0). You should never redifine the variables:

class DCMK
{
public:
DCMK() {Aggression = 1; Agility = 1; }
DCMK(int tempA);
~DCMK() { std::cout << "Destructor Called <<\n"; }
int GetAggression() { return(Aggression); }
void SetAggression(int x) {Aggression = x; }
int GetAgility() { return(Agility); }
void SetAgility(int x) {Agility = x; }
private:
int Aggression;
int Agility;
};



this code will do what you want.

Tom

Share this post


Link to post
Share on other sites
Actually, there are lots of problems here.


class DCMK
{
public:
DCMK() { int Aggression = 1; int Agility = 1; }
// These do not reference the member variables, but instead create locals with
// the same names, assign to them, and promptly throw them away. The same
// problem exists with the other constructor and the set methods.
// ( Having the set and get methods points at a style problem too, but it won't
// break things at least ;) )
// What you want to do is leave off the type names. The member variables are
// "in scope" here and don't need a declaration (which such a line is when
// you include a type name). Just like you can write "return Agility;", so you
// can just do "Agility = 1;".

class Data {
public:
Data() { int pVal = rand(); int pStor = 1; }
// Similarly in here. The names are awful, too. Whoever's telling you how to
// name them so far, please stop. (Not only is this particular style of
// Hungarian notation ugly and useless, you have the wrong prefixes here. ;) )


Data::GetValue(int wot) {
if(int wot = 1) { return pVal; }
else { return wot; }
}
// This will always return pVal, because first of all you assign rather than
// compare, and secondly, you again use a new local "wot" in your if statement
// instead of the passed in one. But then, the intended semantics are totally
// bizarre anyway.

Data::Beast(Data const & other) {
if(other.pStor < pStor)
std::cout << "Worked" << this->pStor << "Sorted Geez!\n";
std::cout << "Brup Brup!!!\n";
return other.pStor;

}
// Only the first line is conditional; the second output will always happen.
// So check your indentation, it's lying to you. :)
// Also, you should declare the return type rather than relying on the implicit
// 'int' return type (the same goes for GetValue()).

int main()
{
DCMK * pPoint = new DCMK; //pointer 1
Data * D1; //pointer 2
// OK, but it doesn't point at anything yet.
Data * D2(4) = new Data; //pointer 3
// The (4) there is wrong. If you want to pass it to the constructor, it goes here:
// Data * D2 = new Data(4);

std::cout << "Hello, world!\n";
std::cout << "Number is " << pPoint->GetAggression() << " for aggression\n";
std::cout << "Number is " << pPoint->GetAgility() << " for aggression\n";
// You get garbage values here for the reasons noted above.

std::cout << "Data is" << D1.GetValue(1) << " Value\n";
// You get a compile error here because D1 is a pointer and you try to use it
// like an actual object; you need -> instead of . the same as you did with
// pPoint. After that, you get a runtime crash, because D1 doesn't actually
// point at an object of that type.

std::cout << "Data is" << D2.GetValue(1) << " Value\n";
std::cout << "Data is" << D2.Beast(2) << " Value\n";
// After fixing the compile errors, these will "work" :\
system ("pause");
// Please learn to run your programs from the command line instead of artificially
// pausing them at the end.
return 0;
// Not needed, but not particularly bad.
};
// No ; needed here, just at the ends of classes. Not particularly bad either though.

Share this post


Link to post
Share on other sites
jeez thanks for all the speedy replies by all, especially for Zahlman who dissected and slaughtered my style lol, you really hoped alot thanks much appreciated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!