Thread: #define question

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are functions, so they shouldn't be placed inside a function.
    Place them outside any functions and call Help from your button control event handler.
    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.

  2. #47
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I have put it like this now. So I have put void Help() outside the button control event handler but this does not either compile :\. Am I doing it wrong.

    Code:
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    			 
    bool ParseIf(const std::string& strToParse);
    bool OperationEqual(const std::string& strToParse, const std::string& strVariable);
    std::map<std::string, bool (*)(const std::string&)> ParseMap;
    std::map<std::string, bool (*)(const std::string&, const std::string&)> ParseMap2;
    std::map<std::string, long> VariablesMap;
    }
    
    	void Help()
    	{
    
    
    	ParseMap["if"] = &ParseIf;
    	ParseMap2["=="] = &OperationEqual;
    	VariablesMap["Number1"] = 1;
    
    	std::string strToParse = "if (Number1 == 1)";
    	std::string::size_type index = strToParse.find(" ");
    	std::string strKeyword = strToParse.substr(0, index);
    	if ( ParseMap.find( strKeyword.c_str() )->second( strToParse.substr(index + 1) ) )
    	; // The if evaluated to true; execute action underneath the if
    	else
    	; // The if evaluated to false; skip everything underneath the if
    	}

  3. #48
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That won't work. It won't find the global variables.

    Code:
    void Help();
    
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    {
    	Help();
    }
    
    bool ParseIf(const std::string& strToParse);
    bool OperationEqual(const std::string& strToParse, const std::string& strVariable);
    std::map<std::string, bool (*)(const std::string&)> ParseMap;
    std::map<std::string, bool (*)(const std::string&, const std::string&)> ParseMap2;
    std::map<std::string, long> VariablesMap;
    
    void Help()
    {
    	ParseMap["if"] = &ParseIf;
    	ParseMap2["=="] = &OperationEqual;
    	VariablesMap["Number1"] = 1;
    
    	std::string strToParse = "if (Number1 == 1)";
    	std::string::size_type index = strToParse.find(" ");
    	std::string strKeyword = strToParse.substr(0, index);
    	if ( ParseMap.find( strKeyword.c_str() )->second( strToParse.substr(index + 1) ) )
    		; // The if evaluated to true; execute action underneath the if
    	else
    		; // The if evaluated to false; skip everything underneath the if
    }
    Last edited by Elysia; 03-13-2008 at 12:42 PM.
    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.

  4. #49
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I have put it excatly like you did above but this does not either compile.

  5. #50
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Compile errors, at the very least, would be helpful.
    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.

  6. #51
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes ofcourse. I get these errors:

    Code:
    error C2039: 'ParseMap' : is not a member of 'std'
    error C2535: 'void Form2::Form1::Help(void)' : member function already defined or declared see declaration of 'Form2::Form1::Help'
    error C4368: cannot define 'ParseMap' as a member of managed 'Form2::Form1': mixed types are not supported
    error C4368: cannot define 'ParseMap2' as a member of managed 'Form2::Form1': mixed types are not supported
    error C4368: cannot define 'VariablesMap' as a member of managed 'Form2::Form1': mixed types are not supported
    error C2678: binary '[' : no operator found which takes a left-hand operand of type 'std::map<_Kty,_Ty>' (or there is no acceptable conversion)

  7. #52
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    C2039: 'ParseMap' : is not a member of 'std'
    Firstly include <map>.

    Code:
    error C2535: 'void Form2::Form1::Help(void)' : member function already defined or declared see declaration of 'Form2::Form1::Help'
    You've already defined Help somewhere else, it seems. Read on.

    Code:
    error C4368: cannot define 'ParseMap' as a member of managed 'Form2::Form1': mixed types are not supported
    error C4368: cannot define 'ParseMap2' as a member of managed 'Form2::Form1': mixed types are not supported
    error C4368: cannot define 'VariablesMap' as a member of managed 'Form2::Form1': mixed types are not supported
    error C2678: binary '[' : no operator found which takes a left-hand operand of type 'std::map<_Kty,_Ty>' (or there is no acceptable conversion)
    Move the code outside of your Form class.

    Code:
    class Form2
    {
    //...
    };
    
    void Help()
    //...
    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.

  8. #53
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I dont really get it to compile anyway.
    I think it would be better for me perheps to buy a book about interpreters as it is kind of complex if you dont know the basics of this like me

    I would be interested about any tips if any of sites or books.
    I did found this link: It sounds to be something to start with.
    http://www.amazon.com/gp/product/047...r_dp_orig_subj

  9. #54
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you should stop messing around with managed code because if it's all native, it will compile fine.
    You'll just barge into problems when writing such a thing later on anyway.
    And I doubt you'll find one on how to express it in code and if you do, I don't think it's going to be managed.
    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.

  10. #55
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes you are probably right. I have put this only to native now like this and have only a few errors.

    If I // these 4 lines like this, it will compile. Is something missing for what I have done.

    // bool ParseIf(const std::string& strToParse);
    // bool OperationEqual(const std::string& strToParse, const std::string& strVariable);
    // ParseMap["if"] = &ParseIf;
    // ParseMap2["=="] = &OperationEqual;


    Code:
    #pragma once
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    #include <cmath>
    #include <algorithm>
    #include <limits>
    #include <ios>
    #include <cstdio>
    #include <map>
    
    using namespace System;
    
    void Help();
    
    
    
    int main(array<System::String ^> ^args)
    {
        Help();
    
        return 0;
    }
    
    bool ParseIf(const std::string& strToParse);
    bool OperationEqual(const std::string& strToParse, const std::string& strVariable);
    std::map<std::string, bool (*)(const std::string&)> ParseMap;
    std::map<std::string, bool (*)(const std::string&, const std::string&)> ParseMap2;
    std::map<std::string, long> VariablesMap;
    
    
    
    void Help()
    {
    	ParseMap["if"] = &ParseIf;
    	ParseMap2["=="] = &OperationEqual;
    	VariablesMap["Number1"] = 1;
    
    	std::string strToParse = "if (Number1 == 1)";
    	std::string::size_type index = strToParse.find(" ");
    	std::string strKeyword = strToParse.substr(0, index);
    	if ( ParseMap.find( strKeyword.c_str() )->second( strToParse.substr(index + 1) ) )
    		; // The if evaluated to true; execute action underneath the if
    	else
    		; // The if evaluated to false; skip everything underneath the if
    }

  11. #56
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are still using a Managed Project, mind you.
    A native project has "int main()" or "int main(int, TCHAR*[]" and no "using namespace System".
    Anyway, again, if you receive compile errors, post 'em. I'm not psychic.
    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.

  12. #57
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I think I have put it right now as above but with int main() etc... I get these compile errors.

    Anyway I think I will wait with writing an interpreter, because I dont really understand the whole process of how this is working. It is to difficult for me I think right now.
    My picture that I get is that it is very difficult. Is it ?
    Perheps it will be more easy when I understand exactly how the interpreter works.
    I dont do that now. Like from step 1 to 10 and here a book maybe is a good id&#233;a.

    Code:
    error LNK2019: unresolved external symbol "bool __cdecl OperationEqual(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?OperationEqual@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function "void __cdecl Help(void)" (?Help@@YAXXZ)
    error LNK2019: unresolved external symbol "bool __cdecl ParseIf(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?ParseIf@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "void __cdecl Help(void)" (?Help@@YAXXZ)
    fatal error LNK1120: 2 unresolved externals

  13. #58
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    These errors are really easy to solve.
    The linker can't find the functions OperationEqual and ParseIf because you haven't put them in your source!
    So put them there and it will compile.
    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.

  14. #59
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes ofcourse you are right. Now it compiles. That much do I know about interpreters. I cant even get the written code in right places

    Seriously, do you think I can manage to write an interpreter. I dont know how difficult this is. Like now I dont know the steps, or how the interpreter exactly works in order to myself begin writing it. First I might understand the steps from the "string" to when it is executable code in words.
    Can I read about this somewhere.
    Like from "if (Number == 1)" wich is a string. The code I could later write, like this string could be 15 lines of code but still with only 10 different variables/vectors but in different combinations. Is this much different then only parsing the "if (Number ==1)"
    Last edited by Coding; 03-13-2008 at 03:44 PM.

  15. #60
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I could write an interpreter, sure. But I don't think I'm going to do it.
    If you look at the code, can you see how it looks for a space, then extracts everything before it? It parses this "keyword" by calling an appropriate function. The "if" keyword calls the ParseIf functions which knows that it's parsing an "if statement." By knowing this, it extracts the operation. Which might be something like ==, != and so on.
    It parses the operation by, again, calling the appropriate function, passing along the variable that it found, which further finds and extracts the number from the string.
    It then executes what the code is actually supposed to do, in my case, by comparing the variable Num1 against 1 and returning true if it did.

    This is the basics of interpreters.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  2. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  3. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 05:01 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM