565 Posted Topics
Re: I think that you have got confused with the syntax of declaration and implementation. So here is an example with most of the common things you will likely need. [Please ask if I have missed something] [code=c++] class Non_Template { // stuff here public: // Class DEFINITION template<typename T> class … | |
Re: First off, you cannot write [icode]fractions fractions :: operator/(fractions f);[/icode] within the definition of a class. Second, you have provided a default constructor that set the fraction to 0/1 . That gives perfectly acceptable results. So which test is wrong?? Third: You have not used references in many of your … | |
Re: The bovious thing to do is to run it in the debugger e.g. gdb or ddd Compile with g++ -g -o Prog prog.cpp, and the run as [icode]ddd Prog[/icode]. When it hits the seg-fault, use up until you see the problem (backtrace in gdb). It is likely that the fault … | |
Re: Well you don't seem to have written a [icode]operator>>[/icode] for class ballTeam. I could be wrong since you didn't include the definition files (.h files). I | |
Re: I am not going to deal with all the things that are wrong here They include: (a) a missing = in a test `else if (opr1 ='(')` (b) stupid reassignment `ifx[i] = ifx[i+1];` but loop on i. (c) Failure to initialize variables and then do a test of two uninitialized … | |
Re: The first problem is that your are returning a string from a function that says it will return a floating point number.. e.g. points has [icode]returns "miss"[/icode] But requires it to return a floating point number. The next thing is that you have added a comma after each 25 in … | |
Re: The problem you have is temporary values and what happens to them: I think this will be clear (I hope :) The problem is that your are taking a copy of the value in the list. This does indeed correctly delete the object. BUT then you do v=0 rather than … | |
Re: This is code absolutely begging for a loop!! Your actual problem is that you test x then y. That is incorrect. (I think) Consider a box with coordinates (0,0) and (10,10). I.e it is a square of area 10. Now what happens when you ball is at (5,23) obviously that … | |
Re: If you are debugging code (and that is what you are doing here), you need to step through the code and check your assumptions. That can be done with a traditional debugger that lets you see each line as it is run, or by putting some output statements in the … | |
Re: First things first: Dont use `system("Pause")` it has been discussed to death here and everywhere on the web. Second: The next problem is that you are using temp1 without initializing it. i.e every time you run the program it could start with a different value and you don't know what … | |
Re: Let use be a little careful here about the sequence of events. First off, if you examine the assembler/machine code that is 100% exactly what is going on. However, (a) that may mean that your compiler is being "smart" (b) the standard says what the effect should be. So now … | |
Re: firstPerson's answer works and solves the problem BUT for a large classes you might prefer to use explicit instantiation. [which gives the beginner a better idea of what is happening (sometimes)]. so you add a [icode]template class HashTable<std::string>;[/icode] at the [B]end [/B] of HashTable.cpp and then you have told the … | |
Re: What about adding a class. Starting with that is a great way to group data together. [code=c++] class UserInfo { private: std::string Name; std::string Pass; // THIS IS NOT SECURE int age; // etc... }; [/code] But you may want to just use a token index e.g. [code=c++] class UserDetails … | |
Re: The reason (for good/bad) is that when the compiler hits instances.inc. It has not seen the definition of graph. If you put include instances at the end of graph.cpp all is fine. Note that you only need the template instance for the class e.g. [icode]template class Graph<int,int,int>;[/icode] since all the … | |
Re: You seem to have two [icode]cin>>x[/icode] in your code at lines 23 and 21. I would delete line 23. I might make count and integer but that is minor. you have remembered to initialize your variables which is good. | |
Re: It doesn't have an inbuilt function (but I am sure they are in libraries) BUT surely it is this [code=c++] int totalMin=890; int min=total % 60 ; int hour = (total / 60) % 24; // etc [/code] not exactly difficult to put into a function. (A fraction of care … | |
Re: Well this looks like a c program (not a c++) so I will assume that you are in C and carry on. The first question is that you have unfortunely added a [icode]\n[/icode] after the %d in the scanf statement. That means if first looks for a number and then … | |
Re: Yes the problem is name reuse. The code compiles fine if you cahgne the name OR you put allocator in a namespace e.g [code=c++] namespace X { template<class T> class Allocator{ public: Allocator(void){} }; } template<class NodeData,class Real=float> class OctNode { private: public: static X::Allocator<OctNode<NodeData,Real> > Allocator; }; [/code] | |
Re: You could use some of the object orientation stuff , i.e. if all the variables in an object are private, then you can add a flag that they have been changed, when they are accessed via the setters/public methods. Alternative, use a checksum. I would keep two numbers (a) the … | |
Re: First off, shurik_r got most of that correct, however, you cannot use typedef for unspecified templates , so you cant write [code=c++] // THIS IS NOT ALLOWED template <class E> typedef node<E>* nodePtr<E>; [/code] However, you can do this should you wish [code=c++] typedef node<int> nodeInt; [/code] The correct way … | |
Re: Basically your template instantiation is in the wrong piece of code. It should be in the Temp.cpp. [B]Why:[/B] To create an instance of a class you have to have all the methods available to the compiler. You can be doing absolutly anything with typename T. So it has to be … | |
Re: Your problem is the subtraction of y. It doesn't do anything. The point about recussion is that in a default case you return a value e.g. [icode]if (y==1) return x;[/icode] but in more general cases you return a simpler function .e.g [icode] if (y>1) return add(x,y-1);[/icode] However, once you get … | |
Re: You can compile everything as separate files BUT to get an executable you need to link all the files required. e.g. in three command do this: [code] g++ -Wall -c Taddmain.cpp g++ -Wall -c Tadd.cpp g++ -Wall -o Taddprog Tadd.o Taddmain.o [/code] or in one coamnd [code] g++ -Wall -o … | |
Re: The problem is that you have a map of type map<string,list<pointer> >. You are doing two thing wrong: [code=c++] std::map<std::string,std::list<Operation *> >::iterator labelPtr; labelPtr = this->labelOPMap.find(currentLabel); // NO CHECK FOR not found e.g. labelPtr==labelOPMap.end() // currentLst does not point to anything: it is not initialized *currentLst = labelPtr->second; ] [/code] … | |
Re: Just a quick note, since you look like you are going to get confused. You really need to be working with double or floats, e.g. 3.4 or 34003.384 or 1.87561e2. Even if you input standard integers sqrt(2) is not an integer. You are also going to have to test for … | |
Re: I think you need either: [icode]while(Choice!='n' && Choice!='N') { } [/icode] Note the && (and). OR: [icode]while(tolower(Choice)!='n') { }[/icode] Also please note you are very unlikely to need the ; after the while unless it is in a [icode]do { } while (condition);[/icode] construction. Whatever you want executed goes in … | |
Re: The normal way to do this is to use a stringstream. Now you have to be VERY VERY careful, with this. The reason is that if you do [code=c++] double Res; std::string A="3.4e5x" std::stringstream IX(A); A>>Res; [/code] This cose will happily set Res equal to 34000. Which is not correct. … | |
Re: Ok there are many many thinks to really really dislike about this code. In particular is the "cleverness". Ok first, it is [icode] int main()[/icode]. That has been said a million times on this forum. Second [icode]for (sigma=ii=0;ii<n;ii++)[/icode] thsi is C++ so (a) reuse index i and declare it in … | |
Re: Note 100% certain I follow what you are trying to do. But some points: (a) C++ is almost space insensitive. The only areas you have to worry are in the middle of keywords e.g. [icode]cla ss A { }[/icode] is not acceptable, nor in the middle of stuff like operator<< … | |
Re: I can't get the code to seg fault but I am running it on linux. However you have [B]many[/B] instances of code like: [icode]if (wrote=false)[/icode] and similar Note: you only have ONE =. This is certainly wrong. Consider the line above. It would set wrote to false and then never … | |
Re: I think siddhants3 although gave a good answer, mis-understood the question by I_Empire. You can do this: [code=c++] #include <iostream> using namespace std; template<typename C> class A { public: A() { } template<typename T> void Print(const T&); }; template<typename C> template<typename T> void A<C>::Print(const T& item) { std::cout<<item<<std::endl; } int … | |
Re: well your compile should give a line number..... What you are not saying is if the error is at run-time or at compile time. Compile time: you may have written something like this [code=c++] const int X(40); // set X to 40 X--; // set X to 39 not allowed. … | |
Re: Initially, the first problem is that you are forgetting/didn't know that if you declare an array of size 5 e.g. [icode]char name[5];[/icode] then that gives you memory locations name[0] to name[4]. But does not give you name[5] that can be used by the program for something else. I have just … | |
Re: Well it is definately wrong. The reason is that the method say it will return an object of set, but -1, EXIT_SUCCESS etc is an integer so it will not compile. The return from a function is based on what will happen later and how bad it is. For example … | |
Re: As I understand the question: Is there an advantage of using size as a template variable rather than as a parameter in the constructor. There are probabily several reasons to prefer either approach. But consider what you CAN'T do with a template parameter relative to a constructor parameter. First: consider … | |
Re: The "best" algorithm depends on the nature of the input and the evaluation metric for "best". Is it only one x that will be compared etc. It sounds like you have words at compile time. If that is the case, then use template meta-programming and create an alphabetical list of … | |
Re: First off please read the formum posting rules about using code tags. e.g. [URL="http://www.daniweb.com/forums/thread78223.html"]http://www.daniweb.com/forums/thread78223.html[/URL] Second. this program is screaming for a overloaded operator* so you can write [icode]Fraction f3=f1*f2;[/icode]. The major point about classes is so that you can use them as abstract items. Small points: Add std::endl on each … | |
Re: First: Memory that is allocated must be deleted. If you allocate memory like f/temp/temp2 and then hit a return you have 100% guaranteed to have a memory leak. Second: you need to post your declaration of flightnode and other classes your are using. Third: Your "end else if" comments just … | |
Re: Sorry came a bit late to this thread. The problem is that you are not using initilizers. Try this: It disposes of the need to have a default constructor `class1()`. The important thing to remember is that the code in the `{ }` after the constructor is run AFTER the … | |
Re: This is simple (I hope). You are using an iterator. The iterator allows the method to change the data. Now since an iterator is a class and your compiler cannot know until link time that the iterator does not change the data, you compiler HAS to assume that the iterator … | |
Re: You have two options. The obvious on is to add a counter to the class. This can be done several ways and then each method and submethod adds to the count. The other (less common and normally less elegant) is to have each method return the number of operations it … | |
Re: Ok despite the code tags that you can (a) get from the advanced edit or simply just type them. [B]Templates:[/B] Ok you need to remember the syntax of a class has not really changed. e.g. [code=c++] // Non-template class class A { void method(); }; void A::method() { return; } … | |
Re: You have forgotten to [B]use[/B] the return value of flip(). If you did this [icode]if (flip()==1)[/icode]. I think you would be a lot better than looking at your loop count varible. If really helps lots to run you code on paper, with a list of variables when the code is … | |
Re: First off I would prefer if you just post your code and not a code snippit as well. Second-- I would like an atempt at the two functions. So instead of helping you directly with them, I am going to comment on your other functions.. Let me start with : … | |
Re: Well it is basically telling you that you have an old version of gcc. (or a very new one that I haven't tested that has changed.) If you write this: [code=c++] // GIVES WARNING: bool MyFunction(const double dist) { if(dist < 1.2) return true; else if (dist>4.0) return false; } … | |
Re: The problem is not within the code you have posted but rather within the code that call it. I guess you have something like this: [code=c++] void draw(const Circle& A) { A.SetSize(3,4,5); // Discards constant! } [/code] | |
Re: First off : The point of a vector is to know the order (otherwize use a set or list etc) . Second : if you have a vector of pointer either : (a) consider an autoptr/boost::shared_ptr [prefered] and just delete the element with a remove_if and then an erase e.g … | |
Re: The problem seem to be that when you get to [icode] Add(1,2,&x);[/icode]. There is no way that the compiler can figure out what T2 should be. So you can get round that by putting [code=c++] Add<int,double>(1,2,&x); [/code] or obviously, you can do it if you have something like [code=c++] template … | |
Re: First use code tags. Second the answer is LOTS and LOTS: including but not limited to (a) putting [icode]using namespace std;[/icode], don't mistype cont and cout. (b) not considering what would happen if you enter -999 (c) Not considering that that you might not want to see all 100 numbers … | |
Re: Let me just add, since i is declared as a double , your loops are complete and test with i==1000 are complete junk. If you add 1 to a double at about 1e16 this is true: [code=c++] // True on some machine and about this value double m=1e16; if (m==m+1) … |
The End.