CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Pointer Problem

  1. #1
    Join Date
    Sep 2009
    Posts
    7

    Pointer Problem

    I want to check if ExpressionPointer1 point at something. But it dosent work I have tried everything I can think about. This is how it looks like:

    if(*binarys[*binaryid].ExpressionPointer1 == NULL){
    ... code ...
    }




    this is how it looks like in the header:

    class Binary : public Expression{
    public:
    string s_operator;
    Expression *ExpressionPointer1;
    Expression *ExpressionPointer2;
    };

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Pointer Problem

    What is 'binarys'? What is 'binaryid'?

    Viggy

  3. #3
    Join Date
    Sep 2009
    Posts
    7

    Re: Pointer Problem

    binarys is a array of Binary : Expression objects. binaryid is just the current number of the array (It increase with 1 each time a add a new object)

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointer Problem

    Ah, "it dosent work". I've encountered that problem dozens of times. Oddly enough the solution was rarely the same twice.

  5. #5
    Join Date
    Apr 2004
    Posts
    102

    Re: Pointer Problem

    Quote Originally Posted by Holloweye View Post
    binarys is a array of Binary : Expression objects. binaryid is just the current number of the array (It increase with 1 each time a add a new object)
    My gut feeling is that you are misusing the * operator, but I can't say for sure without more information.

    We can likely help you if you post how binarys and binaryid are defined (your actual code), and what error you are getting.

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Pointer Problem

    If "ExpressionPointer1" is actually a pointer type, then you're dereferencing that pointer. So you're actually comparing an object of [type pointed to by ExpressionPointer1] to NULL, not a pointer.

  7. #7
    Join Date
    Sep 2009
    Posts
    7

    Re: Pointer Problem

    So here is more info:


    Functions.Cpp:

    Code:
    #include "stdafx.h"
    #include <sstream>
    
    
    void reader(string data, Binary binarys[], Constant constants[], int *binaryid, int *constantid)
    {
    	cout << "Reader";
    
    	int cpri = 0; //CURRENT
    	int hpri = 0; //RIGHT NOW
    	bool done = false;
    	
    
    	while(done == false){
    		if(binarys[*binaryid].ExpressionPointer1 == NULL)
    			next(data,binarys,constants,binaryid,constantid, &cpri, &hpri);
    		else if(binarys[*binaryid].ExpressionPointer2 == NULL)
    			done = true;
    		else{//IF BOTH NULL
    			cout << "NONE";
    		}
    	}
    }

    Binary Expression.h:

    Code:
    #include <string>
    using std::string;
    using namespace std;
    
    class Binary : public Expression{
    public:
    	string s_operator;
    	Expression *ExpressionPointer1;
    	Expression *ExpressionPointer2;
    };

    Constant Expression.h:

    Code:
    class Constant : public Expression{
    public:
    	int number;
    };
    Expression.h:

    Code:
    #include <string>
    using std::string;
    using namespace std;
    
    class Expression{
    public:
    
    };

    ExpressionParser.cpp:

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	string data;
    	fstream filestr;
    
    	int binaryid   = 0;
    	int constantid = 0;
    	Constant constants[99];
    	Binary binarys[99];
    ......
            reader(data,binarys,constants,&binaryid,&constantid);
    .......
    }

    ERROR:
    Code:
    1>------ Build started: Project: ExpressionParser, Configuration: Debug Win32 ------
    1>Compiling...
    1>Functions.cpp
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(90) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(80) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(70) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\streambuf(548) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xmemory(173) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2246) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2050) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'Expression'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(83) : see declaration of 'std::operator =='
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2676: binary '==' : 'Expression' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>Build log was saved at "file://c:\Users\Christer\Desktop\Projects\ExpressionParser\ExpressionParser\Debug\BuildLog.htm"
    1>ExpressionParser - 9 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by Holloweye; September 2nd, 2009 at 12:03 AM.

  8. #8
    Join Date
    Sep 2009
    Posts
    7

    Re: Pointer Problem

    Please Help!

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointer Problem

    Those errors indicate the compiler thinks you're trying to compare a std::string to something. I don't see you doing that, though. Are you positive that you've shown us the current version of all files, and that you don't have older versions lying around which the compiler might be using instead?

  10. #10
    Join Date
    Apr 2004
    Posts
    102

    Re: Pointer Problem

    I'm not sure it will get rid of your problems, but I noticed some design issues:

    1. You have using namespace in your header files. This should only be in your .cpp files. This can cause a myriad of problems (which some of the gurus here can explain much better than I can).

    2. Do your header files all have preprocessor guard statements? Example:
    Code:
    #ifndef SOMEFILE_H
    #define SOMEFILE_H
    
    //header file contents here
    
    #endif
    3. It's not incorrect to pass-by-pointer, but you are using C++ (rather than plain C) anyway, so have you considered using pass-by-reference instead?

    by pointer:
    Code:
    void reader(string data, Binary binarys[], Constant constants[], int *binaryid, int *constantid);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	string data;
    	fstream filestr;
    
    	int binaryid   = 0;
    	int constantid = 0;
    	Constant constants[99];
    	Binary binarys[99];
    ......
            reader(data,binarys,constants,&binaryid,&constantid);
    .......
    
    }
    by reference:
    Code:
    void reader(string data, Binary binarys[], Constant constants[], int &binaryid, int &constantid);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	string data;
    	fstream filestr;
    
    	int binaryid   = 0;
    	int constantid = 0;
    	Constant constants[99];
    	Binary binarys[99];
    ......
            reader(data,binarys,constants,binaryid,constantid);
    .......
    
    }

  11. #11
    Join Date
    Sep 2009
    Posts
    7

    Re: Pointer Problem

    I fixed the things you told me jefranki.
    I tried with this:
    if(*binarys[*binaryid]->ExpressionPointer1 == NULL)

    Now I get this error:
    Code:
    1>------ Build started: Project: ExpressionParser, Configuration: Debug Win32 ------
    1>Compiling...
    1>ExpressionParser.cpp
    1>Functions.cpp
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2819: type 'Binary' does not have an overloaded member 'operator ->'
    1>        c:\users\christer\desktop\projects\expressionparser\expressionparser\binaryexpression.h(8) : see declaration of 'Binary'
    1>        did you intend to use '.' instead?
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(15) : error C2232: '->Binary::ExpressionPointer1' : left operand has 'class' type, use '.'
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(16) : error C2664: 'next' : cannot convert parameter 4 from 'int' to 'int *'
    1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(17) : error C2819: type 'Binary' does not have an overloaded member 'operator ->'
    1>        c:\users\christer\desktop\projects\expressionparser\expressionparser\binaryexpression.h(8) : see declaration of 'Binary'
    1>        did you intend to use '.' instead?
    1>c:\users\christer\desktop\projects\expressionparser\expressionparser\functions.cpp(17) : error C2232: '->Binary::ExpressionPointer2' : left operand has 'class' type, use '.'
    1>Generating Code...
    1>Build log was saved at "file://c:\Users\Christer\Desktop\Projects\ExpressionParser\ExpressionParser\Debug\BuildLog.htm"
    1>ExpressionParser - 5 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointer Problem

    What happens if you do this:
    Code:
    if(!(binarys[*binaryid]->ExpressionPointer1))

  13. #13
    Join Date
    Aug 2009
    Posts
    1

    Re: Pointer Problem

    Quote Originally Posted by Holloweye View Post


    Functions.Cpp:

    Code:
    #include "stdafx.h"
    #include <sstream>
    
    
    void reader(string data, Binary binarys[], Constant constants[], int *binaryid, int *constantid)
    {
    	cout << "Reader";
    
    	int cpri = 0; //CURRENT
    	int hpri = 0; //RIGHT NOW
    	bool done = false;
    	
    
    	while(done == false){
    		if(binarys[*binaryid].ExpressionPointer1 == NULL)
    			next(data,binarys,constants,binaryid,constantid, &cpri, &hpri);
    		else if(binarys[*binaryid].ExpressionPointer2 == NULL)
    			done = true;
    		else{//IF BOTH NULL
    			cout << "NONE";
    		}
    	}
    }
    Is there any specific reason for using int* at 'binaryid' and 'constantid'? I would rather use plain ints.
    Otherwise you have to change the header of the method 'next'. I assume it takes integer and not integer pointers.
    Code:
    next(data,binarys,constants,binaryid,constantid, &cpri, &hpri);
    //change
    next(data,binarys,constants,*binaryid,*constantid,&cpri,&hpri);
    But like I mentioned before I don't know why you're using pointers as your arguments. At the moment I can't see a reason for that.

  14. #14
    Join Date
    Sep 2009
    Posts
    7

    Re: Pointer Problem

    Quote Originally Posted by Lindley View Post
    What happens if you do this:
    Code:
    if(!(binarys[*binaryid]->ExpressionPointer1))
    No erros but... I know that the object's ExpressionPointer1's dont point at anything but it still print "NONE"

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Pointer Problem

    Why don't you breakpoint on the relevant line and make *sure* they don't point to anything.

Page 1 of 2 12 LastLast

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