| | |
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:
Solved Threads: 0
I'm having trouble figuring out how I would replace a value in my vector once it is found by the if statement.
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
last but not least, is this makefile correct?
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
#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?
C++ Syntax (Toggle Plain Text)
# 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
Last edited by railmaster7; Oct 5th, 2009 at 9:23 pm.
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 2
0
#2 Oct 6th, 2009
Try like this:
But you should be using an unordered_set to hold a dictionary anyway.
C++ Syntax (Toggle Plain Text)
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.
Last edited by rbv; Oct 6th, 2009 at 1:09 am.
![]() |
Similar Threads
- vector template help (C++)
- counting and replacing words from .txt (C++)
- Crazy Leak...plz help (C++)
- STL funciton return vector of map iterators (C++)
- How can i find out the Max and Min value from a Vector? (C++)
Other Threads in the C++ Forum
- Previous Thread: memory allocation doubt
- Next Thread: Reading an outside file mortgage calculator
Views: 259 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





