Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <string>
#include <vector>
using namespace std;

#include "symboltable.h"

void SymbolTable::insert(string variable, int value)
{
    const Symbol& symbol = Symbol(variable, value);
    elements.push_back(symbol);
}

int SymbolTable::lookUp(string variable) const
{
    for (size_t i = 0; i < elements.size(); i++)
        if (elements[i].variable == variable)
             return elements[i].value;
    return -1;
}

void SymbolTable::init()
{
	if(elements.size() > 0)
	{
		for (int i = elements.size(); i > 0; i--) {
			elements.pop_back();
		}
	}
}


What I have tried:

for (int i = elements.size(); i > 0; i--) { *** Error- Implicit conversion loses integer precision: 'std::vector<SymbolTable::Symbol>::size_type' (aka 'unsigned long') to 'int'
Posted
Updated 9-Oct-22 22:12pm

Replace
Quote:
for (int i = elements.size(); i > 0; i--) {
elements.pop_back();
With
C++
for (size_t i = 0; i < elements.size(); ++i) {
			elements.pop_back();
 
Share this answer
 
Comments
Greg Utas 10-Oct-22 9:35am    
That's a 5.
CPallini 10-Oct-22 11:31am    
Thank you!
The size method returns a size_type value, the definition of which is implementation dependant - but in your case is would appear to resolve to unsigned long from the error message.

Since you are allocating that value to a int the implicit conversion involved would potentially discard 33 bits, and the warning is telling you that it could affect your code.

Use an explicit cast instead, and you'll be fine (unless you exceed 2147483647 elements, which seems unlikely).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900