I'm having trouble figuring out how I would replace a value in my vector once it is found by the if statement.

void ExprTree::setVariable(const string& s, double d)
   {
      vector<Variable>::iterator pos;
      for(pos = varList.begin(); pos!= varList.end(); ++pos)
      {
         if(pos->data = s) ; //here is where i'm stuck
         else if(pos == varList.end()) varList.push_back(Variable(s,d));
      }
      
   }

also, when I try to compile my code I get

ExprTree.h:31: error: ISO C++ forbids declaration of vector with no type
ExprTree.h:31: error: invalid use of ::
ExprTree.h:31: error: expected ; before < token

#include <iostream>
#include <string>


struct TNode
   {
      std::string data;
      TNode* left; 
      TNode* right;
      TNode(const std::string&);
   };
   
class ExprTree
   {
      private:
              TNode* root;
              std::vector<Variable> varList; //here is the problem.

              void inTrav(TNode* current) const;
              void postTrav(TNode* current) const;
              void destroyTree(TNode*);
              double evaluate(TNode*) const;

      public:       
              ExprTree();
              ExprTree(const std::string&);
              ExprTree(const ExprTree&);
              ~ExprTree();
              void clear();
              ExprTree& operator=(const ExprTree &);
              void build(const std::string&);
              double evaluate() const;
              void printInorder() const;
              void printPostorder() const;
              void printLevel() const;
              void setVariable(const std::string&, double);
              double getVariable(const std::string&) const;
              void printVariables() const;
              
              
   };

#endif

last but not least, is this makefile correct?

# Compiler variables
CCFLAGS = -ansi -Wall

# Rule to link object code files to create executable file
assign3: assign3.o ExprTree.o Variable.o
	g++ $(CCFLAGS) -o assign3 assign3.o ExprTree.o Variable.o

# Rules to compile source code files to object code
assign3.o: assign3.cpp ExprTree.h
	g++ $(CCFLAGS) -c assign3.cpp

Variable.o: Variable.cpp Variable.h
	g++ $(CCFLAGS) -c Variable.cpp

ExprTree.o: ExprTree.cpp ExprTree.h
	g++ $(CCFLAGS) -c ExprTree.cpp

# Pseudo-target to remove object code and executable files
clean:
	-rm *.o assign3

Edited 7 Years Ago by railmaster7: n/a

Try like this:

void ExprTree::setVariable(const string& s, double d)
   {
      vector<Variable>::iterator pos;
      for(pos = varList.begin(); pos!= varList.end(); ++pos)
      {
         if(pos->data == s) { //two equals for comparison
             *pos = Variable(s,d); // one equals for assignment
             return; // you're wasting time continuing to loop after you found a match
         }
      }
      varList.push_back(Variable(s,d));
   }

But you should be using an unordered_set to hold a dictionary anyway.

Edited 7 Years Ago by rbv: n/a

What is Variable? If this is a class that you have written make sure that you are including the file that holds the class in the file that is giving that compile error. When you try to declare your vector it seems like the program doesn't know about the Variable class.

This question has already been answered. Start a new discussion instead.