#include <iostream> int ival1 int ival2=1 int summe /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main() {
Code: void change(pixel_t *pixel, int w, int h, pixel_t *buffer) {
int average, sum; int i; pixel_t *pointer;
Everything else works fine. I keep getting an error when I call the convert function. It says "expected expression before pixel_t" and "too few arguments to function "change". I know that everything else in the main is working.
I have a program that rolls two dice however many times the user specifies and counts the occurrences of each total (2-12). I have to compare the results of the random rolls to the expected outcome and give the percentage of error between the two.
I have an example that tells me the sum to expected outcome are as follows if I roll 36 times: 2/1, 3/2, 4/3, 5/4, 6/5, 7/6, 8/5, 9/4, 10/3, 11/2, 12/1 but I don't know how to put that into code to get the expected outcome for X amount of rolls.
I've just recently started to learn C++, and I'm encountering some errors I can't seem to figure out.
InventoryItem.h:
Code: #pragma once class InventoryItem { public: InventoryItem(string name, int amount); ~InventoryItem(void); string getName(void); int getAmount(void);
#include <iostream> #include <initializer_list> using namespace std; void doSomething(std::initializer_list<int> list) { } int main() { doSomething({2,3,6,8}); return 0; }
I write a small piece of code like above. But I can not compile it successfully. I try it both with and without the line "using namespace std", but nothing worked.
The error list: Symbol 'initializer_list' could not be resolved
In order to test catching exceptions from an initializer list, I deliberately did bad practice by hard coding an argument to a ctor that would cause a std::bad_allocto be thrown. Obviously better practice is to send a variable, but that would cause a compile error, so I hard coded a value.
The program I wrote creates Prime Numbers up to a specified limit which is an argument to the ctor of type std::size_t. The program works fine IMO, using g++ in cygwin:
$ time ./PrimesExe Limit is 2000000 148933 Primes Created
real 0m1.210s user 0m1.123s sys 0m0.046s
Now when I send something invalid like a negative number or something too big for std::size_t, the program seems to run indefinitely, when compiled with g++ under cygwin. I haven't tested it yet on Linux.
However, if I do the same on VS2013 express, it takes about 15 seconds to print the expected caught exception message. I was not expecting it to take so ridiculously long compared to the reasonable amount of work involved in doing primes up to 2 million.
I have read up about what is involved in catching exceptions: stack unwinding, keeping track of what needs to be destroyed etc. But this is 1 object with 1 ctor argument, no Base classes or any other complications. So why such a long or indefinite amount of time?
This whole example is probably contrived, and I am wondering whether exceptions is the right tool for this - it is similar to the divide by zero problem, or could be considered a programming error to call a ctor with a bad argument?
Also, catching an exception thrown by an initalizer list seems a bit awkward in that one seems to have enclose the creation of the object and all subsequent uses of it (and any code in between ) in the same try block, otherwise it goes out scope. I suppose I could try to write a wrapper function that returns a smart pointer reference to a valid object, but I would have to test the validity of it's return too. That's the awkward part - there is probably a better way?
Are there any recommended ways of recovering from initializer list exception, that is, to allow the user to enter a new hopefully valid value and try to create the object again?
I working on an assignment that processes an array of structs. In the main function I am attempting to declare an array of Author structures with 3 elements. It is supposed to be initialized to set all of the string fields (the names and book titles) to "NONE", and the double fields (the prices) to zero. This is supposed to be done in one statement, not using loops. Here is what I have.
//prototype for function to print the content of array on screen void showInfo(Author a[], int size);
[Code] .....
I was under the impression that an array can only hold the values of one data type. So doubles and strings in the same array doesn't make sense to me. However, that's the example my teacher drew up. The error keeps telling me that there are too many initializer values.
Code: extended initializer lists only available with -std=c++11 or -std=gnu++11 that I belive is due to having initialized a class array in the constructor somewhat like this: Code: class xpto { public; xpto():num{25,25}{} int num[2]; };
Since the code I'm developing is not meant to be compiled only by me and I want to ensure there are no incompatibilities with other machines I would like to kow whats the best way to initialize the array that is not c++11 dependent.
Is there no way to do it directly on the constructor "pre-instructions" (don't know the correct designation for the initialization section)? or do I have to put the instructions on the constructor body.
I keep getting an error here and cant quite figure out why,
Code: else if (mainMenu == 3){ cout << "Please make a selection" << endl << " 1) Withdraw from account" << endl << " 3) Back to main menu" << endl; cin >> withdrawMenu;
I wrote a program, that generates 20 random integers, and stores them in an array. Then I decided, to build more functions to it, i.e why not have it to display the lowest integer of an array. I created my function,
Code: int minValue( int field[ ] )
and got my code in side, which (technically) works. In my main() function I'm calling
Code: printf( "The smallest value of an array is: %d ", minValue( field[] ) );
and I'm getting an error trying to compile it.
Code: randomArray.c:62:74: error: expected expression before ']' token printf( "The smallest value of an array is: %d ", minValue( field[] ) );
After compiling it is giving error as : foo.cpp: In function ‘int Foo_max(Foo*)’: foo.cpp:26:37: error: expected primary-expression before ‘int’ foo.cpp:26:46: error: expected primary-expression before ‘int’
This first Dowhile loop is saying "expected unqualified-id" at the do and while parts. What does that mean
do { do { do { std::cout << "Give me a test result or grade from 0 to 100 for class"<< 1 << std::endl; std::cin >> mathaverage[mathtestcount]; loopresult=1;
I have a template class (A) that derives from a either a base class that has a member (_A1.m) or not (_A0), based upon it's template parameter. class (A) has a member function (fn) returns the value of (m) as it understands what (m) is.
However, this gives different results compared with a non-template class in a similar scenario. I'm expecting that if derived from _A1, that m should be taken from the base class scope and if derived from _A0, it should be taken from the global one.
Here is the code for your amusement:
int m = 5; class _A0 { public: _A0(int) {
[Code] ....
This compiled using g++ 4.5.3 and 4.6.3 with the same results: Global value of m is: 5 B0 class has no internal m member. Object resolves m internally with value 5 B1 class has internal m member. Object resolves m internally with value 3 A<_A0> class has no internal m member. Object resolves m internally with value 5 A<_A1> class has internal m member. Object resolves m internally with value 5
#pragma once #include <iostream> class CBox // Derived class { public: // Constructor explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv){}
[Code] .....
Before the program ends, at return 0;
I would expect the CBox destructor to be called 3 times but it is being called 6 times? Why? Also in this code:
#pragma once #include <iostream> class CBox // Derived class { public: // Constructor explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv){}
[Code] .....
Why is the destructor called 3 times? When have you really destroyed a CBox? Doesnt emplace only create it and store it, then thats it?
[URL] .....
For pushback:
void push_back(value_type&& _Val) {// insert by moving into element at end if (_Inside(_STD addressof(_Val))) {// push back an element size_type _Idx = _STD addressof(_Val) - this->_Myfirst;
I'm trying to perform a simple input operation, and check to make sure that whatever the user input is a valid input (is within data type bounds, is correct data type...). But when I get to ignoring the input, I keep getting an error.
unsigned short num; while (true) { std::cin >> num; if (std::cin.fail()) { num = 1;