Your function has a return value (string). The function must return a value in any case. Now if the program doesn't enter your if, it will reach the end of the function, where there's no return value => error.
The compiler is smart enough for this, though:
Code:
int abs(n) {
if (n > 0)
return n;
else
return -n;
}
There is an equivalent to the repeat ... until loop:
int i=0;
do {
++i;
cout << i << endl;
} while (i<10);
There needs to be a ; after while.
I prefer to use the normal while loop when possible, as it's faster to understand than varying while / do while loops.
Does anyone have a suggestion how to solve this in a better way?
I have an abstract class A containing an std::vector<> of Mat (other class; not important here), with an enum specifying what sort of information the vector holds at each index.
That's it exactly. When you calculate the denominator separately, you get to huge numbers, and dividing them results in numerical errors (might even get an overflow with big numbers).
So to calculate ( n *( n-1) * ... * (n-k+1) ) / ( k * (k-1) * ... * 1 ), you need to always take a pair of multiplicands together:
Than, in the overloaded functions, you just ignore the arguments that concern other classes (CurrentAccount ::create will ignore SavingAccountAr gument1), and give any data when calling this function.
...
1) Mostly I'm trying to make things tidier, as there are already a lot of libraries that I have to link every time. It would work just putting the library at the end of the path, just fells overdone. I was wondering if there's a better way.
2) Yes, that's about what I'm trying to do. A library would be good, but when I create a library, are the libraries it uses linked into it? I thought...
Well, the important thing about using polymorphism (which the exercise is about, I suppose), is that the base class ( Account ) can only have functions and members that every class that inherits from it has too.
So, if you want to be able to call Accounts[0]->create( ... ), you must have one function create which takes the same arguments for each class. Only then it is possible to call the function for any Account (CurrentAccount...
As I see it, you have two functions void create(...) = 0;
You implement one of them in CurrentAccount and one in SavingAccount, but you would have to implement both functions in both classes to stop them from being abstract.
A class is abstract until all functions virtual ... =0; of all base classes are implemented.
Why do you need both functions create to be virtual .. = 0; ?
Linking static library into a .o-file instead of the executable
Hello
I'm not sure this is the right place to ask, hope it's not too far off.
I'm using a big external library in my project, but need it only in one class. I'd like to link the library into the .o-file.
I realise this is not how .o-files normally work, but it would be handy to realize it in this case. I would then only have to link to my own .o-file in every executable I need it, and not search the library...
Leave a comment: