Replacing vector val and delaration help.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 5
Reputation: railmaster7 is an unknown quantity at this point 
Solved Threads: 0
railmaster7 railmaster7 is offline Offline
Newbie Poster

Replacing vector val and delaration help.

 
0
  #1
Oct 5th, 2009
I'm having trouble figuring out how I would replace a value in my vector once it is found by the if statement.

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

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

  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. struct TNode
  6. {
  7. std::string data;
  8. TNode* left;
  9. TNode* right;
  10. TNode(const std::string&);
  11. };
  12.  
  13. class ExprTree
  14. {
  15. private:
  16. TNode* root;
  17. std::vector<Variable> varList; //here is the problem.
  18.  
  19. void inTrav(TNode* current) const;
  20. void postTrav(TNode* current) const;
  21. void destroyTree(TNode*);
  22. double evaluate(TNode*) const;
  23.  
  24. public:
  25. ExprTree();
  26. ExprTree(const std::string&);
  27. ExprTree(const ExprTree&);
  28. ~ExprTree();
  29. void clear();
  30. ExprTree& operator=(const ExprTree &);
  31. void build(const std::string&);
  32. double evaluate() const;
  33. void printInorder() const;
  34. void printPostorder() const;
  35. void printLevel() const;
  36. void setVariable(const std::string&, double);
  37. double getVariable(const std::string&) const;
  38. void printVariables() const;
  39.  
  40.  
  41. };
  42.  
  43. #endif



last but not least, is this makefile correct?

  1. # Compiler variables
  2. CCFLAGS = -ansi -Wall
  3.  
  4. # Rule to link object code files to create executable file
  5. assign3: assign3.o ExprTree.o Variable.o
  6. g++ $(CCFLAGS) -o assign3 assign3.o ExprTree.o Variable.o
  7.  
  8. # Rules to compile source code files to object code
  9. assign3.o: assign3.cpp ExprTree.h
  10. g++ $(CCFLAGS) -c assign3.cpp
  11.  
  12. Variable.o: Variable.cpp Variable.h
  13. g++ $(CCFLAGS) -c Variable.cpp
  14.  
  15. ExprTree.o: ExprTree.cpp ExprTree.h
  16. g++ $(CCFLAGS) -c ExprTree.cpp
  17.  
  18. # Pseudo-target to remove object code and executable files
  19. clean:
  20. -rm *.o assign3
Last edited by railmaster7; Oct 5th, 2009 at 9:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: rbv is an unknown quantity at this point 
Solved Threads: 2
rbv rbv is offline Offline
Newbie Poster
 
0
  #2
Oct 6th, 2009
Try like this:

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

But you should be using an unordered_set to hold a dictionary anyway.
Last edited by rbv; Oct 6th, 2009 at 1:09 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 64
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 6th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 259 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC