编译器错误C2664和xmemory0

时间:2016-12-07 01:09:14

标签: c++ visual-c++

我正在尝试查明此错误的位置来自何处。 C2664是一个参数转换问题,在我的编码中没有显示为错误,而是用xmemory0编写的东西(或者至少,这就是我如何解释它)。我理解它抱怨的部分原因是它被要求从分配器转换为字符串。我没有在这个文件中使用分配器。

错误如下:

C2664   'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty>      
&&)': cannot convert argument 1 from       
'std::vector<std::string,std::allocator<_Ty>>' to 'const std::string &'  
Project1    c:\program files (x86)\microsoft visual studio 
14.0\vc\include\xmemory0, Line 737

相关代码:

/* 
Interpreter class implementation: utilizes the lex scanner class as
well as the expression evaluator to interpret simple statements like 
read display and assignment statements

!!: indicates current problems or things that are going to be problems.
*/

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include "lexScanner.h"
#include "expEvaluator.h"
#include "interpreter.h"
using namespace std;


void Interpreter::executeProgram(vector<string>& sourceProgram)
{
    // ****************************************************
    // PARSING THE INFORMATION:
    // Intialize all containers for storage of broken down string components
    // Resize al appropriate vectors to the size of the source program
    // ****************************************************

    // !!: This probably wont work. Source program is going to be magnitudes 
    // smaller than the amount of category tokens
    // but it worked in the last programming project soooo. .. .


    vectOfTokenVects myVectOfTokenVects;
    vectOfCategoryVects myVectOfCategoryVects;
    perLineCategoryVector myPerLineCatVect;


    // Send for parsing
    LexicalScanner::getLexicalInfo(sourceProgram,myVectOfTokenVects,myVectOfCategoryVects);


    //Display the lexical information for user's means

    cout << "Here is a preview of the source code to be executed. . \n";

    // Create a string-to-float map that serves as a symbol table to maintain
    // the names of variables and their values during execution

    // !!: figure out how to store items in the map
    floatVarValueTable varTable;

    // ****************************************************
    // EXECUTING A READ STATEMENT: 
    // Obtain numerical input from the user and 
    // store it properly in the corresponding variable in the map as a symbol
    // table.
    // ****************************************************

    // Cycle through the tokens, searching for the three categories

    // If the catToken is keyword AND IS read, go ahead and execute a cin 
    // If the catToken is keyword AND IS display, go ahead and execute a cout 

    // If the catToken is an assignment statement, figure out how far the 
    // math expression goes. Try to compute the line. Search comma to comma?
    // ****************************************************
    for (int i = 0; i < myVectOfCategoryVects.size(); i++)
    {
        // Start out with the largest net: search for keywords
        for (int j = 0; j < myVectOfCategoryVects[i].size(); j++)
        {
            if (myVectOfCategoryVects[i][j] == KEYWORD)
            {

                // SCENARIO 1: READ STATEMENT
                // Have the user enter the value for the variable to be added to vartable.
                if ((myVectOfTokenVects[i][j] == "Read") || (myVectOfTokenVects[i][j] == "READ") || (myVectOfTokenVects[i][j] == "read"))
                {
                    float reportedValue;
                    cout << "Please enter a value for: " << myVectOfTokenVects[i][j + 1] << endl;
                    cin >> reportedValue;
                    string dumpedString = myVectOfTokenVects[i][j + 1];
                    varTable.emplace(dumpedString, reportedValue);
                }


                // SCENARIO 2: DISPLAY STATEMENT
                // Search the vector (token vector) for things to display. 
                if (myVectOfTokenVects[i][j] == "Display" || myVectOfTokenVects[i][j] == "display" || myVectOfTokenVects[i][j] == "DISPLAY")
                {
                    // Search the vector continiously until you reach a comma or semicolon. Both comma and semicolon indicate the end of the display statement.
                    int currentPosition = j+1;

                    for (int j = currentPosition; j > myVectOfTokenVects[i].size(); i++)
                    {
                        if (myVectOfCategoryVects[i][j] != COMMA)
                        {
                            cout << myVectOfTokenVects[i][j];
                        }
                        // We've got something after the comma, prob an expression. Send it to expression evaluator
                        if (myVectOfCategoryVects[i][j] == COMMA)
                        {
                            expVector infixExpression;
                            float expValue;
                            // Start filling up the expression vector
                            for (int k = j; k > myVectOfTokenVects[i][j].size(); k++)
                            {
                                infixExpression.push_back(myVectOfTokenVects[i][k]);

                            }
                            // Take the newly formed expression vector and send it to the evaluator. It does pass by reference, so the values in the vartable and infixexpression are going to be updated
                            ExpressionEvaluator::infixEvaluator(infixExpression, varTable, expValue);
                            cout << expValue;
                        }
                    }

                }
            }

            // ****************************************************
            // SEARCHING FOR VARIABLES
            // If the token in question is an assignment operator, go ahead
            // and insert the item before and after it.

            // Note: This leaves a hole stating that if there is a floating
            // equals sign somewhere with nothing before or after it, it
            // could cause a out of bounds error.

            // Note: We should also check for the presence of an equation.
            // Evaluate the RHS of the assignment op

            if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
            {
                // SCENARIO 1: SIMPLE EVALUATION
                // If it's a simple 3 part assignment statement: ie:
                // name (=) value, go ahead and store it
                if (myVectOfCategoryVects[i][j + 2])
                {
                    // Make sure the assignment op has a var name
                    if (myVectOfCategoryVects[i][j - 1])
                    {
                        // Store both the name and value
                        varTable.emplace(myVectOfTokenVects[j - 1], myVectOfTokenVects[j + 1]);
                    }
                }
            }

            // SCENARIO 2: COMPLEX EVALUATION
            // More complex evaluation: sending the RHS to the evaluator
            // NOTE: postfix evaluator needs to be fixed. .
            if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
            {
                // Semicolon out of normal place, assume incoming expression
                if (myVectOfCategoryVects[i][j + 2] != SEMICOLON)
                {
                    // NOTE: The placement of this is intentional. . .it guarentees that they get cleared on OOS
                    expVector infixExpression;
                    float expValue;

                    // Start with whatever is before the assignment op, start adding items to the infixExpression vector. Lookout for semicolon
                    for (int j = 0; myVectOfCategoryVects[i][j] != SEMICOLON; i++)
                    {
                        infixExpression.push_back(myVectOfTokenVects[i][j]);
                    }
                    // Take the newly formed expression vector and send it to the evaluator. It does pass by reference, so the values in the vartable and infixexpression are going to be updated
                    ExpressionEvaluator::infixEvaluator(infixExpression, varTable, expValue);
                }
            }
        }
    }
}

相对定义:

//****************************************************************************
// define a mnemonic name for the type to store tokens of one line of statement
//****************************************************************************
typedef vector<string> perLineTokenVector;


//****************************************************************************
// define a mnemonic name for the type to store tokens of lines of statement
//****************************************************************************
typedef vector<perLineTokenVector> vectOfTokenVects;


//****************************************************************************
// define a mnemonic name for the type to store categories of tokens 
// of one line of statement
//****************************************************************************
typedef vector<tokenCategory> perLineCategoryVector;


//****************************************************************************
// define a mnemonic name for the type to store categories of tokens 
// of one line of statement
//****************************************************************************
typedef vector<perLineCategoryVector> vectOfCategoryVects;

编辑:错误似乎存在于此区域

if (myVectOfCategoryVects[i][j] == ASSIGNMENT_OP)
            {
                // SCENARIO 1: SIMPLE EVALUATION
                // If it's a simple 3 part assignment statement: ie:
                // name (=) value, go ahead and store it
                if (myVectOfCategoryVects[i][j + 2]=SEMICOLON)
                {
                    // Make sure the assignment op has a var name
                    if (myVectOfCategoryVects[i][j - 1] = STRING_LITERAL)
                    {
                        string stringDump = myVectOfTokenVects[i][j - 1];

                        // Store both the name and value
                        varTable.emplace(stringDump, myVectOfTokenVects[i][j + 1]);
                    }
                }
            }

构建输出

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>  interpreter.cpp
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(67): warning C4018: '<': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(70): warning C4018: '<': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(94): warning C4018: '>': signed/unsigned mismatch
1>c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(106): warning C4018: '>': signed/unsigned mismatch
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): error C2664: 'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty> &&)': cannot convert argument 2 from 'std::string' to 'const float &'
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=float
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: Reason: cannot convert from 'std::string' to 'const float'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,std::string&,std::string&>(_Objty *,std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1>              _Objty=std::pair<const std::string,float>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,std::string&,std::string&>(_Objty *,std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1>              _Objty=std::pair<const std::string,float>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(996): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::string&,std::string&>(std::allocator<_Other> &,_Objty *,std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Tree_node<std::pair<const std::string,float>,void *>>,
1>              _Ty=std::pair<const std::string,float>,
1>              _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1>              _Objty=std::pair<const std::string,float>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(995): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::string&,std::string&>(std::allocator<_Other> &,_Objty *,std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<std::_Tree_node<std::pair<const std::string,float>,void *>>,
1>              _Ty=std::pair<const std::string,float>,
1>              _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1>              _Objty=std::pair<const std::string,float>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(889): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,std::string&,std::string&>(_Ty *,std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1>              _Ty=std::pair<const std::string,float>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(887): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,std::string&,std::string&>(_Ty *,std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Other=std::_Tree_node<std::pair<const std::string,float>,void *>,
1>              _Ty=std::pair<const std::string,float>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(1076): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::string&,std::string&>(std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=float,
1>              _Traits=std::_Tmap_traits<std::string,float,std::less<std::string>,std::allocator<std::pair<const std::string,float>>,false>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xtree(1076): note: see reference to function template instantiation 'std::_Tree_node<std::pair<const _Kty,_Ty>,void *> *std::_Tree_comp_alloc<_Traits>::_Buynode<std::string&,std::string&>(std::string &,std::string &)' being compiled
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=float,
1>              _Traits=std::_Tmap_traits<std::string,float,std::less<std::string>,std::allocator<std::pair<const std::string,float>>,false>
1>          ]
1>  c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(145): note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::string&,std::basic_string<char,std::char_traits<char>,std::allocator<char>>&>(std::string &,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=float,
1>              _Pr=std::less<std::string>,
1>              _Alloc=std::allocator<std::pair<const std::string,float>>
1>          ]
1>  c:\users\alfar\google drive\school\programminglanguages\programming 4a\programming 4a vs2\project1\project1\interpreter.cpp(145): note: see reference to function template instantiation 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::string&,std::basic_string<char,std::char_traits<char>,std::allocator<char>>&>(std::string &,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' being compiled
1>          with
1>          [
1>              _Kty=std::string,
1>              _Ty=float,
1>              _Pr=std::less<std::string>,
1>              _Alloc=std::allocator<std::pair<const std::string,float>>
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

编辑3 似乎问题在于我传递给地图的安置功能。 myVectOfTokenVects是字符串向量的奇特字符串向量。 map.emplacement()的第二个参数需要一个浮点值,myVectOfTokenVects无法提供。问题已经找到。

随访: 我如何解析字符串值以将其转换为map.emplacement()的浮点数?

0 个答案:

没有答案