Thread: Database Class.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Database Class.

    This is a very very simple database software I am creating.



    Code:
    #include <string>
    #include <list>
    
    
    
    
    
    class BUBBELdb {
    
    
    public:
    	std::string query; // our query string
    	
    
    	int ParseQuery( std::string query )
    	{
    
    		(*this).query = query;
    
    		std:: list< std::string > keywords;
    
    		/**
    
    		I was going to make a totally different header file for these keywords ( using #define ), but what the heck.
    		
    		
    		*/
    
    		// --- List keyword types
    		keywords.push_back("GET");   // To grab columns from tables
    		keywords.push_back("FROM");  // To grab tables
    		keywords.push_back("JOIN");  // To Join other tables
    		keywords.push_back("WHERE"); // To find if a certain column contains a certain value
    		keywords.push_back("SET");	 // To set columns to certain values
    		keywords.push_back("UNION"); // To cluster our GET clauses
    		// --- end List keyword types
    	
    
    		for ( int i = keywords.front(); i<keywords.back();/* .end() doesn't work either. */ i++ )
    		{
    
    			if ( keywords[i].find(keywords[i].c_str(), NULL ) )
    			{
    
    				return 1;
    
    			}
    
    		}
    
    
    		return 0;
    	}

    Errors:
    Code:
    1>------ Build started: Project: database, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>main.h(41) : error C2440: 'initializing' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'int'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>,
    1>            _Ax=std::allocator<char>
    1>        ]
    1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>main.h(41) : error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'int'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\list(1310) : see declaration of 'std::operator <'
    1>main.h(41) : 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 'int'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(150) : see declaration of 'std::operator <'
    1>main.h(41) : error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'int'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(140) : see declaration of 'std::operator <'
    1>main.h(41) : 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 'int'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\string(130) : see declaration of 'std::operator <'
    1>main.h(41) : 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 'int'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2236) : see declaration of 'std::operator <'
    1>main.h(41) : 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 'int'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xutility(2046) : see declaration of 'std::operator <'
    1>main.h(41) : 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 'int'
    1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(84) : see declaration of 'std::operator <'
    1>main.h(41) : error C2677: binary '<' : no global operator found which takes type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>,
    1>            _Ax=std::allocator<char>
    1>        ]
    1>main.h(44) : error C2676: binary '[' : 'std::list<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>        with
    1>        [
    1>            _Ty=std::string
    1>        ]
    1>main.h(44) : error C2228: left of '.find' must have class/struct/union
    1>main.h(44) : error C2676: binary '[' : 'std::list<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>        with
    1>        [
    1>            _Ty=std::string
    1>        ]
    1>main.h(44) : error C2228: left of '.c_str' must have class/struct/union
    1>Build log was saved at "file://Debug\BuildLog.htm"
    1>database - 13 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Most of this seems to be complaining about operators, so should I operator overload it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does the BUBBELdb class model?

    At the moment, the main problem seems to be that you are not reading documentation correctly. You should have understood that front() returns a reference to the front of the container. For a std:: list< std::string >, this is a reference to a std::string, which certainly is not an int. You probably wanted to use begin(), but begin() for a std::list<std::string> returns a std::list<std::string>::iterator, not an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    What does the BUBBELdb class model?

    At the moment, the main problem seems to be that you are not reading documentation correctly. You should have understood that front() returns a reference to the front of the container. For a std:: list< std::string >, this is a reference to a std::string, which certainly is not an int. You probably wanted to use begin(), but begin() for a std::list<std::string> returns a std::list<std::string>::iterator, not an int.
    I KEEP PRESSING BACK AND THE MESSAGE IS BEING CLEARED D:

    Look, I tried begin aswell it gave me errors, and I haven't done iterators yet.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    141
    By the way, I used size too. ;P

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    I also tried vectors.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I KEEP PRESSING BACK AND THE MESSAGE IS BEING CLEARED D:
    Keep a text editor open for composing your posts.

    Look, I tried begin aswell it gave me errors, and I haven't done iterators yet.
    The body of your loop is wrong as well, but I cannot figure out what you had in mind when you wrote it. Doubly linked lists do not allow for random access, or even faster than linear time access, so operator[] is not overloaded for std::list. Consequently, it makes sense to use iterators here, e.g.,
    Code:
    #include <list>
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::list<std::string> keywords;
        keywords.push_back("GET");   // To grab columns from tables
        keywords.push_back("FROM");  // To grab tables
        keywords.push_back("JOIN");  // To Join other tables
        keywords.push_back("WHERE"); // To find if a certain column contains a certain value
        keywords.push_back("SET");	 // To set columns to certain values
        keywords.push_back("UNION"); // To cluster our GET clauses
    
        for (std::list<std::string>::iterator i = keywords.begin(), end = keywords.end();
            i != end; ++i)
        {
            std::cout << *i << std::endl;
        }
    }
    By the way, I used size too. ;P
    You need to start reading and thinking instead of just doing guess and check.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by laserlight View Post
    You need to start reading and thinking instead of just doing guess and check.
    And Laserlight cranks that one deep and out of the park!!!

    With the addition of "and running to the forums as soon as it doesn't work."
    Last edited by rags_to_riches; 08-04-2008 at 10:38 AM. Reason: Department of Redundancy Department fix-up.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    Keep a text editor open for composing your posts.


    The body of your loop is wrong as well, but I cannot figure out what you had in mind when you wrote it. Doubly linked lists do not allow for random access, or even faster than linear time access, so operator[] is not overloaded for std::list. Consequently, it makes sense to use iterators here, e.g.,
    Code:
    #include <list>
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::list<std::string> keywords;
        keywords.push_back("GET");   // To grab columns from tables
        keywords.push_back("FROM");  // To grab tables
        keywords.push_back("JOIN");  // To Join other tables
        keywords.push_back("WHERE"); // To find if a certain column contains a certain value
        keywords.push_back("SET");	 // To set columns to certain values
        keywords.push_back("UNION"); // To cluster our GET clauses
    
        for (std::list<std::string>::iterator i = keywords.begin(), end = keywords.end();
            i != end; ++i)
        {
            std::cout << *i << std::endl;
        }
    }

    You need to start reading and thinking instead of just doing guess and check.
    Do I have to do it in an int main?

    Well .size() does return how many elements are in a list/vector so whats your point in guessing? :P


    By the way, I'll just look into the documentation some more, and read up on iterators so.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by rags_to_riches View Post
    And Laserlight cranks that one deep and out of the park!!!

    With the addition of "and running to the forums as soon as it doesn't work."
    Well it's not like I know immediately what the errors are. :P

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do I have to do it in an int main?
    Why do you think that this is restricted for use in the global main function?

    Well .size() does return how many elements are in a list/vector so whats your point in guessing? :P
    You are guessing how the list traversal is done. This is clear from the fact that you tried begin() and front(), end() and size(), without giving consideration into the type of i. If you carefully consulted a reference, even an online reference like cppreference.comp, you would have had a much better idea of what to do.

    The error was not very cryptic. It says that the compiler "cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'int'". This points to a mistake related to the type of i.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    Why do you think that this is restricted for use in the global main function?


    You are guessing how the list traversal is done. This is clear from the fact that you tried begin() and front(), end() and size(), without giving consideration into the type of i. If you carefully consulted a reference, even an online reference like cppreference.comp, you would have had a much better idea of what to do.

    The error was not very cryptic. It says that the compiler "cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'int'". This points to a mistake related to the type of i.
    Actually I did read up on begin, front, end, size, but it's not like I read the whole thing. :P

    Well thank you for telling me what the error is, but it was cryptic for a nooby xD.

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Quote Originally Posted by bobbelPoP View Post
    Well it's not like I know immediately what the errors are. :P
    You're not expected to, but it would help if you read your compiler output carefully and read documentation about language features before you try to use them.

    www.cppreference.com

    The above site has some great information on various aspects of C/C++. Try take a look at the section on lists - it shows you what operators are defined for that template (if you look you will find that the [] operator is not shown there), the member function prototypes for lists and many simple code snippets to demonstrate typical use.

    I would also suggest that you write out step by step in very basic terms what you want to achieve before you try to implement it. That way when you run into problems you can help us to help you by telling us what you are trying to do, showing us your attempt and then telling us what's not working. Also, post short but complete sections of code where possible - that code snippet won't compile because you didn't copy a closing brace for the class.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    141
    Well I did fix my problem, so thanks guys!


    Code:
    class BUBBELdb {
    
    
    public:
    	std::string query; // our query string
    	
    
    	int ParseQuery( std::string query )
    	{
    
    		(*this).query = query;
    
    		std:: list< std::string > keywords;
    
    		/**
    
    		I was going to make a totally different header file for these keywords ( using #define ), but what the heck.
    		
    		
    		*/
    
    		// --- List keyword types
    		keywords.push_back("GET");   // To grab columns from tables
    		keywords.push_back("FROM");  // To grab tables
    		keywords.push_back("JOIN");  // To Join other tables
    		keywords.push_back("WHERE"); // To find if a certain column contains a certain value
    		keywords.push_back("SET");	 // To set columns to certain values
    		keywords.push_back("UNION"); // To cluster our GET clauses
    		// --- end List keyword types
    	
    
    		for ( std::list< std::string >::iterator i = keywords.begin(), end = keywords.end();
    			i != end; i++ )
    		{
    
    			if ( query.find((*i), NULL) )
    			{
    
    				return 1;
    
    			}
    
    		}
    
    
    		return 0;
    	}
    Last edited by bobbelPoP; 08-04-2008 at 11:01 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Notes:
    1) It's bad practice to define a member in a class and the EXACT same name for something that you take by argument in a function. Separate them somehow - preferably be prefixing members with m_.
    2) (*this). is the same as this->.
    3) You don't need to use this-> to access members of the class. At least if you follow note 1.
    4) You can use Firefox & Session Manager. It keeps saving form data, so if you accidentally hit back or the browser crashes, (most) of it will still be there! Hit the forward button!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    Notes:
    1) It's bad practice to define a member in a class and the EXACT same name for something that you take by argument in a function. Separate them somehow - preferably be prefixing members with m_.
    2) (*this). is the same as this->.
    3) You don't need to use this-> to access members of the class. At least if you follow note 1.
    4) You can use Firefox & Session Manager. It keeps saving form data, so if you accidentally hit back or the browser crashes, (most) of it will still be there! Hit the forward button!

    I know number 2, and number 4 doesn't work, I do forward it back, but it's still clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2009, 04:13 PM
  2. Friend class?
    By legit in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2008, 12:32 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM