I am going through a tutorial on using Direct3D in C++, and it's been going great and i've been at it for a couple of hours, this is the first bug. I am obviously happy i have had no problems what so ever until now but this wcstombs_s stuff does not seem to work properly at all, i wrote like this:
error = wcstombs_s((size_t)&stringLength, (char)m_videoCardDescription, (size_t)128, (const wchar_t)adapterDesc.Description, (size_t)128);
According to the tutorial i should not even have to do the (varType) thing, but since it didn't want to work at all, i though it would be necessary to make sure i was following the format instructions Visual Studio gave me, doesn't work still, gives me the message
no instance of overloaded function "wcstombs_s" matches the argument list
argument types are: (size_t, char, size_t, wchar_t, size_t)
[EDIT]I also tried doing this to make sure i did nothing wrong with the formatting of the variables:
size_t st0;
char c0;
size_t st1;
const wchar_t wct0 = 0;
size_t st2;
int test = wcstombs_s(&st0, c0, st1, wct0, st2);
Guess what? Did not work either!
This is driving me nuts, please help.
Take for example:
int main(void){
numberComparator comparator1;
comparator1.setA(78.321);
comparator1.showA();
comparator1.setB('c');
comparator1.setB("Yes");
comparator1.setB(124.213);
comparator1.showB();
comparator1.setB(12);
return 0;
}
Instead of saying comparator1 over and over again, can I do something shorter?
I understand that this doesn't really change much about how the program works, but it does make it easier to work around with testing a class I make.
I am doing overloading so that for an assortment of inputs into my comparator, my program can handle them without making the results go crazy. In this case, I want the input to be an int, but what if the input isn't?
The answer could be lying around the internet, but as my title may infer, I do not know how to state the question.
You are looking for something like with keyword which is part of, for example, Pascal language.
Unfortunately, C++ doesn't provide similar feature. Using the references, one can shorten the name of the class and somewhat alleviate the pain, i.e.
Comparator comparator1;
...
{
Comparator& cr = comparator1;
cr.a();
cr.b();
cr.c();
}
It depends. If numberComparator has a "fluent" interface, then each member function will return a reference to *this, and you can write:
comparator1
.setA(78.321)
.showA()
.setB('c')
.setB("Yes")
.setB(124.213)
.showB()
.setB(12);
Note that this is a bitch to debug by step-into (you have to step into every function until you get to the one you are interested in).
The alternative of course is "use a shorter name".
int main(void){
numberComparator c1;
c1.setA(78.321);
c1.showA();
c1.setB('c');
c1.setB("Yes");
c1.setB(124.213);
c1.showB();
c1.setB(12);
return 0;
}
There is really no point in having a particularly long name if it is limited in scope to a few lines. For a local variable, if it isn't limited in scope to a few lines, your function is probably too long.
using b_arr = bool [10];
i have written this code in code::blocks on windows. i have seen many other posts on the same error but do not understand what is wrong in my code.
i actually want to use the resulting user-defined data type for a function to return a boolean array created inside the function. i also do not understand how i will do that(return a boolean array). please help.
i am a novice in programming and coding, so it would be nice have some explanations
I started learning c++ about 3 weeks ago after 2 years of java. It seems so different but im getting there. my lecturer is a lovely guy but any time i ask a question as to why something is that way or this way. he just responds "because it is".
Theres lots of comments in the code below with a few random questions, but the main problem is im getting two build errors, one says the arraytotal has not been initialized (even though i found a value for it) and the other says an external reference in main.
would anyone mind reading the code and answering a few comments within, and maybe the overall problem im having?
#include<string>
#include<fstream>
#include<ostream>
using namespace std;
//double decimals[5] ={2,4,6,8,10};
const int arraySize = 5;
// does an arraySize have to be const always? is it so it doesnt channge after the array has been created?
//double decimals[arraySize];
/*
this array is being created in the function averageN() but why?
cant i just create it up top and reference it in?
*/
// why do you have to write the name of the function up here before you even create it?
double averageN();
int main()
{
averageN();
return 0;
}
// why does the array have to be created here?
double averageN(double decimals[arraySize])
{
double average;
double arrayTotal;
for (int i = 0; i<5;i++)
{
// fills with random numbers from 0 - 10
decimals[i] = (0+(rand()%10));
}
// find the total of all the elements in the array
for (int i = 0; i < arraySize;i++)
{
double currentElement = decimals[i];
arrayTotal = (currentElement+arrayTotal);
//arrayTotal +=decimals[i]) ;
}
// return the average
average = (arrayTotal/arraySize);
return 0.0;
}
// does an arraySize have to be const always? is it so it doesnt channge after the array has been created?
Yes, it has to be const, moreover, it must be a constant expression, which means its size must be known at compile-time (not at runtime).
If you want to resize arrays, then the best is to use the standard container std::vector. Or use dynamically allocated arrays if you want a fixed-size array, but the size is not known until runtime
/*
this array is being created in the function averageN() but why?
cant i just create it up top and reference it in?
*/
if you speak about decimals, then no, it is a global variable, you can use it from anywhere.
// why do you have to write the name of the function up here before you even create it?
You must declare any name in C++ prior to its use. Since you call this function in main, it must be at least declared beforehand. You can also provide the definition (body) before main.
// why does the array have to be created here?
Oops, it appears that there's a big mixup in your code. As a matter of fact, you have 2 functions named averageN, one is averageN that takes no parameters, other is AveraeN taking an array of double.You never defined the first one, just declared.
Errors:
doubleTotal is not initialized. Well it is not
double arrayTotal; change to
double arrayTotal = 0.0;
unresolved extenal in main - that's the AverageN function in main you are caling. You never wrote a body for it. You created a function that takes an array, which wasn't useful since your array is global. Just delete the array parameter from AverageN definition.
HTH
P.S. Read S. Lippmann's C++ Primer. It's the best book for beginners ther is for C++. IMO :)
const gives the compiler a clue that the item should not be changed and if the code attempts it then the compiler can flag an error.
the function name is mentioned before the actual declaration the main() function needs to reference it before the compiler has actually come to compile it (as it later on in the code file). You can move the whole function before the main() to avoid this.
double averageN(double decimals[arraySize]) is saying this function takes an array. It doesn't say that it create the array. If you look in the function, it takes the array, and adds calculated values into it (decimals[i] = (0+(rand()%10))). This function also calculates an average over the array and returns that as a double.
So to answer your big question what's wrong - read the last point and look at the call you are making -averageN(); - can you see how this is not the correct call?
OK, here's your only assignment into arrayTotal:
arrayTotal = (currentElement+arrayTotal);
Now, what is the value of arrayTotal after this assignment? Well, it depends on its value before the assignment. What was its value before the very first assignment? You don't know. It could be anything because you never gave it an initial value.
const int arraySize = 5;
// does an arraySize have to be const always? is it so it doesnt channge after the array has been created?
C++ basically supports two kinds of arrays: fixed-size arrays, that are declared as type name[size], and dynamic arrays that are allocated with new[].
For fixed-size array you must provide their size to the compiler so that it can set aside enough memory for the array. As the size must be known to the compiler, you can only specify it with a const variable or a literal.
Although it is possible to create dynamically allocated arrays yourself, by invoking new[], this will give you some headaches getting the memory management right. It is better to use existing classes that do this for you, like std::vector.
//double decimals[arraySize];
/*
this array is being created in the function averageN() but why?
cant i just create it up top and reference it in?
*/
You can create it up here, but that would give anybody access to the array. For a small program like this that is not a big problem, but consider that there are a dozen other files that can also access the array and change it at unexpected moments.
It is basically the same question as to why you don't make all classes and members public in Java: to limit who has access.
// why do you have to write the name of the function up here before you even create it?
double averageN();
You have to declare the function before you can use it. C++ has the requirement that all names you use must have been declared to the compiler before their first use, and the compiler will read a file in sequential order from the top to the bottom.
Note that this declaration does not match the function definition you give below. As C++ supports function overloading, the two are considered different functions, because they accept different parameters (none versus a pointer)
int main()
{
averageN();
return 0;
}
// why does the array have to be created here?
double averageN(double decimals[arraySize])
{
This does not create an array. Instead it specifies that the function expects to be called with a pointer argument (the arraySize is completely ignored and you can't pass an array to a function in C++, so the parameter is adjusted to read double *decimals).
To get a working program, you need to change the two lines above to
double averageN()
{
double decimals[arraySize];
double average;
double arrayTotal;
Both average and arrayTotal are not initialised. That means they will start with some unknown value.
For average, thatis not a problem, because the first thing you do with it is assign a new value. But for arrayTotal, you are adding values to it, so you must let it start with a known value.
for (int i = 0; i<5;i++)
{
// fills with random numbers from 0 - 10
decimals[i] = (0+(rand()%10));
}
// find the total of all the elements in the array
for (int i = 0; i < arraySize;i++)
{
double currentElement = decimals[i];
arrayTotal = (currentElement+arrayTotal);
//arrayTotal +=decimals[i]) ;
}
// return the average
average = (arrayTotal/arraySize);
return 0.0;
}
My quick answers without double checking (its been awhile since I've developed in C++) are:
arraytotal has not been initialized
I suspect that your compiler flags this as an error to make sure you do it. If you don't, you can't be sure what it will be initialized to. Traditionally for debug builds, C/C++ initialized memory to some debug value to help identify uninitialized variables. Set arrayTotal = 0 when it is initialized and that should go away. (Best practice)
e.g. double arrayTotal = 0;
external reference in main
I suspect this is because your prototype for averageN does not match the method defined later. A prototype needs to include the types of the parameters as well as the return type. Change the prototype from double averageN(); to double averageN(double []); and I believe that will fix that problem.
does an arraySize have to be const always? is it so it doesnt channge after the array has been created?
Since you are using it to define the size of the array passed into averageN, yes. Setting the size of the array like this requires a constant value.
This array is being created in the function averageN() but why?
cant i just create it up top and reference it in?
It is not being created in averageN. It is a formal parameter to averageN. The caller of averageN needs to provide the appropriate variable and pass it in. Then from within the method, you access it through decimals.
why do you have to write the name of the function up here before you even create it?
This is the function prototype. It is necessary if the function is referenced in code before the function is defined. This could be resolved in other ways as well, such as moving the definition of averageN before all uses of it.
Couple of problems:
Your forward declaration of averageN is not correct
Code:
double averageN();
The supplied version below takes a parameter.
Your declaration of averageN does not quite work
Declaring parameters of type array is not intuitively obvious.
Normally people let the array degrade to a pointer to the array and pass a length as a second parameter.
Code:
double averageN(double *decimals, int arraySize)
If you want to only pass arrays of a specific size you need to do so by reference:
double averageN(double (&decimals)[arraySize])
Your call to averageN() in main.
You are passing zero parameters. Now this matches the forward declaration but not the actual definition.
Result I would change the code like this:
extern double averageN(double (&decimals)[arraySize]);
int main()
{
double data[arraySize];
averageN(data);
return 0;
}
// why does the array have to be created here?
double averageN(double (&decimals)[arraySize])
{
In addition to the technical details of the other answers, instead answering the complaint in your first paragraph: one way to get quality answers to "why" C++ questions is to ask in one of the Usenet groups, because, like with StackOverflow there are Real Experts hanging out there, but unlike StackOverflow even a beginner's question is likely to get an answer from a member of the standardization committee, and if you're lucky even from those who have penned the standard (like Andrew Koenig or currently Pete Becker). In earlier times Bjarne was there too. But recent years he hasn't posted much.
Basic novice "why" questions:alt.comp.lang.learn.c-c++. Francis Glassborow hangs out there. He's a committe member and author of several successful C++ introductory books. He also knows a bit of math, and since the posting frequency there is low (what with StackOverflow!), you are almost assured that any halfway interesting question will be answered almost immediately -- and correctly -- by Francis, in a snap. :-)
General questions about the C++ language: comp.lang.c++and/or comp.lang.c++.moderated. The latter group is moderated and has a charter. The moderation reduces noise -- e.g. no spam -- but adds delay. Some committee members prefer to post mostly in the unmoderated group (e.g. James Kanze), some, like Pete Becker and Howard Hinnant, post in both groups, and some well known experts, e.g. Andrei Alexandrescu, now apparently only post in the moderated group.
Questions about what the standard means, reports about errors in the standard and so on (in earlier times this was also where you formally reported defects in the standard): [comp.std.c++]. This is also a moderated group, and unfortunately the mod delay is now almost unbearably long. But as a novice you're probably less interested in the formal details, and more interested in rationales and explanations, for which [comp.lang.c++] and [comp.lang.c++.moderated] are great groups (I think StackOverflow's main advantage is when you're wondering "what's the bug in this code" or questions that in principle can be resolved by just reading the relevant documentation).
Finally, I've linked to Google Groups which provides a web based interface, but you can access these groups more directly from a Usenet client like Thunderbird (or e.g. the Opera browser, which has a built-in client). All you have to do in order to access Usenet via a local client is to configure the client, telling it about a server, like the free EternalSeptember. Or AIOE.
Cheers & hth.,
– Alf
One thing nobody seems to have commented on: your return statement at the end of averageN is wrong. You say you're returning the average, then you calculate the radius, and then return 0.0;. Try return average; or return arrayTotal/arraySize;.
I've been trying run Insure++ with some scientific code and it reports many errors, although to be fair it officially does not support K&R C and I don't know what having a lot of K&R functions has done to its evaluation process. The C and C++ code it is testing is being run in a DLL invoked from a WPF application.
One error report that puzzles me is the following, which I'm confident is safe code but am trying to work out why it thinks is an error (it does work). I'd be interested if anyone has an insight into why this might be an error condition.
[MacImagePlot.c:984] **READ_OVERFLOW**
SetCursorQD(*GetCursorQD(watchCursor));
Reading overflows memory: GetCursorQD(watchCursor)
bbbbb
| 4 | 4 |
rrrrr
Reading (r) : 0x5639d164 thru 0x5639d167 (4 bytes)
From block (b) : 0x5639d160 thru 0x5639d163 (4 bytes)
gWatchCursor, declared at WPFMacGraphics.cpp, 418
for some very simple code.
typedef int Cursor;
typedef Cursor* CursPtr;
typedef CursPtr* CursHandle;
CursHandle GetCursorQD (short cursorID);
void SetCursorQD (const Cursor *crsr);
enum {
....
watchCursor = 4
};
// file globals
Cursor gWatchCursor=watchCursor;
CursPtr gWatchCursorPtr = &gWatchCursor;
CursHandle GetCursorQD (short cursorID)
{
if (cursorID==watchCursor) // this is actually the only case ever called
return &gWatchCursorPtr;
return 0;
}
I'm not familiar at all with the tools you're talking about, but have you verified that your GetCursorQD function is returning the pointer you expect and not NULL/0?
Perhaps something wonky happened with your enum definition for watchCursor (such as it being declared differently elsewhere, or it picking up a local variable instead of the enum).
I hate to say it but I suspect your problem is going to be the lack of some arcane function modifiers needed to ensure that data on the stack isn't getting munged when crossing the DLL boundary. I'd suggest writing a simple app that replicates the code but does it all in one module and see if Insure++ still detects an error. If it doesn't, get ready to wade through __declspec documentation.
I assume that the following line is the Problem:
if (cursorID==watchCursor)
cursorID is defined as short (usually 2 Bytes)
watchCursor is part of a enum and thus of type int (4 Bytes on a 32Bit OS)
This actually is not a problem. The compiler will cast one of both parameters correctly, as far as the enum value will not exceed a 2 Byte range.
By my experience all static (as well as runtime-) code analysis tools report many false positives (i tried some of them). They of course help, but it takes quite a while to assert false positives from real bugs.
Like Soapbox, I am not familiar with Insure++.
But looking at the code, it is admittedly a bit confusing...so
That typedef makes CursHandle effectively a pointer to pointer to int...
CursHandle is a pointer of type CursPtr
CursPtr is a pointer of type Cursor
Cursor is typedef'd to type int
yet in the GetCursorQD, you are using a 'double address of' int? The reason I say 'double address' is the function is returning a address of gWatchCursorPtr (&gWatchCursorPtr) of type CursHandle, which in turn is a global variable which is a address of gWatchCursor (&gWatchCursor) which is of type Cursor.
Your definition of the return type for the function does not match up with the global variable name's typeof despite the typedef's...that's what my thinking is...
Hope this helps,
Best regards,
Tom.