CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  3. #3
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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

  4. #4
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: Whats wrong with this program

    Quote Originally Posted by alanjhd08 View Post
    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

  5. #5
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: Whats wrong with this program

    Quote Originally Posted by GCDEF View Post
    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]

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  7. #7
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: Whats wrong with this program

    Quote Originally Posted by GCDEF View Post
    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?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Whats wrong with this program

    No, it's scope is only the if statement.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Whats wrong with this program

    Quote Originally Posted by vkash View Post
    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

  10. #10
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: Whats wrong with this program

    Quote Originally Posted by Paul McKenzie View Post
    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.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Whats wrong with this program

    Quote Originally Posted by vkash View Post
    ...
    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

  12. #12
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: Whats wrong with this program

    Quote Originally Posted by VictorN View Post
    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.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Whats wrong with this program

    Quote Originally Posted by vkash View Post
    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.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Whats wrong with this program

    Quote Originally Posted by vkash View Post
    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,...)?

    Quote Originally Posted by vkash View Post
    ... 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

  15. #15
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    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
  •  





Click Here to Expand Forum to Full Width

Featured