|
-
May 23rd, 2011, 07:32 AM
#1
Whats wrong with this program
What is wrong with this code
https://docs.google.com/document/d/1...l=en_US&pli=1#
errors are here
https://docs.google.com/document/d/1...edit?hl=en_US#
As i have studied new pointer are did not destroyed in the that function till you did not delete that or that program did not ends but if it is so then why it is complaining for these errors?
Thanks to all answerers.
-
May 23rd, 2011, 08:08 AM
#2
Re: Whats wrong with this program
It doesn't compile. p is only defined in the scope of one if statement. Undefined in the others.
The second link is some kind of logon page do Google Docs. Just paste your code in here using code tags.
Last edited by GCDEF; May 23rd, 2011 at 08:10 AM.
-
May 23rd, 2011, 08:11 AM
#3
Re: Whats wrong with this program
Hi,
I can't see the list of errors, but for what you are trying to do you need to declare p as static, and make sure it's in scope throughout your function.
You have a few other logic errors as well, try stepping through the code once you get it running to see how the various combinations of arguments are treated.
Alan
-
May 23rd, 2011, 09:57 AM
#4
Re: Whats wrong with this program
 Originally Posted by alanjhd08
Hi,
I can't see the list of errors, but for what you are trying to do you need to declare p as static, and make sure it's in scope throughout your function.
sorry for interruption now i have managed this issue please see it now.
You have a few other logic errors as well, try stepping through the code once you get it running to see how the various combinations of arguments are treated.
Alan[/QUOTE]
what do you mean i don't understood
-
May 23rd, 2011, 10:17 AM
#5
Re: Whats wrong with this program
 Originally Posted by GCDEF
It doesn't compile. p is only defined in the scope of one if statement. Undefined in the others.
HEY HEY it should not go out of scope see new updated code. See it now------ Build started: [/QUOTE][/QUOTE]
The second link is some kind of logon page do Google Docs. Just paste your code in here using code tags.[/QUOTE]
-
May 23rd, 2011, 10:28 AM
#6
Re: Whats wrong with this program
I don't understand your last post, but
Code:
if((create==1)&&(incr==0))
int *p=new int(1);
p only exists in that one statement.
And again, please post your code in here using code tags.
-
May 23rd, 2011, 10:41 AM
#7
Re: Whats wrong with this program
 Originally Posted by GCDEF
I don't understand your last post, but
Code:
if((create==1)&&(incr==0))
int *p=new int(1);
p only exists in that one statement.
And again, please post your code in here using code tags.
Code
#include<iostream>
using namespace std;
int func(int,int );
int main()
{
func(1,0);
func(0,0);
func(0,0);
func(0,0);
func(0,0);
func(0,0);
func(0,0);
func(0,0);
cout<<"Number of times function called"<<func(0,1)<<endl;
func(1,1);
return 0;
}
int func(int create, int incr)
{
if((create==1)&&(incr==0))
int *p=new int(1);
if((create==0)&&(incr==0))
(*p)++;
cout<<*p<<endl;
if((create==0)&&(incr==1))
return ++(*p);
if(create==1)
if(incr==1)
delete p;
return 0;
}
errors
------ Build started: Project: cpp, Configuration: Debug Win32 ------
Compiling...
gfhj.cpp
d:\study\alll about c++ programming\c++ program\cpp\cpp\gfhj.cpp(25) : error C2065: 'p' : undeclared identifier
d:\study\alll about c++ programming\c++ program\cpp\cpp\gfhj.cpp(27) : error C2065: 'p' : undeclared identifier
d:\study\alll about c++ programming\c++ program\cpp\cpp\gfhj.cpp(29) : error C2065: 'p' : undeclared identifier
d:\study\alll about c++ programming\c++ program\cpp\cpp\gfhj.cpp(32) : error C2065: 'p' : undeclared identifier
d:\study\alll about c++ programming\c++ program\cpp\cpp\gfhj.cpp(32) : error C2541: 'delete' : cannot delete objects that are not pointers
Build log was saved at "file://d:\study\alll about C++ programming\c++ program\cpp\cpp\Debug\BuildLog.htm"
cpp - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
hey i am confused from your statements it's scope should inside braces of function?
did i need to revise scope again?
-
May 23rd, 2011, 10:44 AM
#8
Re: Whats wrong with this program
No, it's scope is only the if statement.
-
May 23rd, 2011, 11:38 AM
#9
Re: Whats wrong with this program
 Originally Posted by vkash
Code
After over 30 posts, you should already know how to use code tags.
That is not a code tag. You should have seen that the tags do not work when you preview your message.
This:
Code:
if((create==1)&&(incr==0))
int *p=new int(1);
is no different than this:
Code:
if((create==1)&&(incr==0))
{
int *p=new int(1);
}
Now do you see the problem?
Regards,
Paul McKenzie
-
May 23rd, 2011, 07:02 PM
#10
Re: Whats wrong with this program
 Originally Posted by Paul McKenzie
After over 30 posts, you should already know how to use code tags.
That is not a code tag. You should have seen that the tags do not work when you preview your message.
This:
Code:
if((create==1)&&(incr==0))
int *p=new int(1);
is no different than this:
Code:
if((create==1)&&(incr==0))
{
int *p=new int(1);
}
Now do you see the problem?
Regards,
Paul McKenzie
Thanks paul you tell me the root reason.
actually i write this code to use this property of the new pointer that they did not destroyed in memory till end or delete call.
But finally for such works i should use static variable or reference or global variable.
Once again thanks.
-
May 24th, 2011, 01:20 AM
#11
Re: Whats wrong with this program
 Originally Posted by vkash
...
But finally for such works i should use static variable or reference or global variable.
Once again thanks.
Wrong!
Finally you have to use some type of STL (or MFC if you use MFC) container like std::vector or MFC CArray...
Note that it is C++, not just the old C!
Victor Nijegorodov
-
May 25th, 2011, 05:00 AM
#12
Re: Whats wrong with this program
 Originally Posted by VictorN
Wrong!
Finally you have to use some type of STL (or MFC if you use MFC) container like std::vector or MFC CArray...
Note that it is C++, not just the old C!
I am using VC++ express i am thinking to download a pirated copy of visual studio 2010 should i download pirated copy.
-
May 25th, 2011, 05:07 AM
#13
Re: Whats wrong with this program
 Originally Posted by vkash
should i download pirated copy.
I guess you didn't read the board's rules on illegal activity. Of course the answer is no, you shouldn't download pirated copies of software.
Secondly, what made you all of a sudden want VS 2010? What's wrong with just Visual Express? Nothing that you're doing requires anything but a basic C++ compiler.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 25th, 2011 at 05:09 AM.
-
May 25th, 2011, 05:09 AM
#14
Re: Whats wrong with this program
 Originally Posted by vkash
I am using VC++ express
Did you want to say that *your* VC++ express installation does not contain the STL (standard template library)? And you cannot use any std objects (like std::string, std::vector,...)? 
 Originally Posted by vkash
... i am thinking to download a pirated copy of visual studio 2010 should i download pirated copy.
The "pirated copy" problems are not discussed on CG boards.
Victor Nijegorodov
-
May 25th, 2011, 05:15 AM
#15
Re: Whats wrong with this program
guys i know that VC++ express is enough for begginer like me.
I will not download pirated copy.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|