why my code is giving undeclared variable error? - c++

include "stdafx.h"
#include <vector>
#include <iostream>
std::vector<int> biggest;
std::vector<int>vector1;
std::vector<int>vector2;
int main(){
biggest = [vector2[0],0]; //wrong initialization
for (int apply = 0; apply < (vector2.size()); apply++) {
if (biggest[0] < vector2[apply + 1]) {
biggest[0] = vector2[apply + 1];
biggest[1] = apply + 1;
}
}
Error C2065 'apply': undeclared identifier.why this error is occurring as i have already defined apply variable in for loop. error should be in initialization of biggest(vector).why wrong compiler code?
even intellisense is not giving me error is it a visual studio bug?

apply is in scope in the for loop body, so be assured, the error is not there. But you are aware that apply is out of scope after the loop body?
I'm only answering this because your use of
vector2.size() - 1
will give you hell if vector2 is empty, as the above will wrap around to a large unsigned value and your program will fail spectacularly! Use
for (int apply=0; apply + 1 < vector2.size(); apply++) {
instead.

Related

Undeclared Identifier with all the right includes

Everytime I try to build the program I get this error:
error C2065: 'DepositoFresco' : undeclared identifier
This happens with every instance I create of DepositoFresco, DepositoNormal and Deposito. DepositoNormal and DepositoFresco are subclasses of Deposito (virtual class). I have all the right includes so I don't know what's causing this.
The error occurs in the class 'Armazem' where I instantiate several of these to insert in vectors and such. Here's the code:
Armazem::Armazem(int nF, int nN, int nPF, int nPN, int distMaxi, int distMini) : depositos(), distancia(), graphStlPath <Deposito*, int>() {
distMax = distMaxi;
distMin = distMini;
for (int i = 0; i < nF; i++) {
DepositoFresco* df = new DepositoFresco(random(1, 20), (float)random(1000, 10000), nPF);
depositos[i] = df;
}
for (int j = nF; j < nF + nN; j++) {
DepositoNormal* dn = new DepositoNormal(random(1, 20), (float)random(1000, 10000), nPN);
depositos[j] = dn;
}
preencherMatriz();
}
Also, Armazem is a subclass to another template class called GraphStlPath but I don't think the problem is here.
EDIT: Here are the includes:include "Deposito.h"
include "DepositoFresco.h"
include "DepositoNormal.h"
include "graphStlPath.h"
include <vector>
include <map>
include <stdlib.h>
include <stdio.h>
include <time.h>
include <typeinfo>
include <iostream>
include <fstream>
include <string>
Any help finding the problem is really apreciated.
My psychic debugging powers tell me that you have a cycle in your includes, and header guards or #pragma once are kicking in, making code disappear for the compiler.
That, or you are not respecting namespaces. But the first one is more likely.

if and else without braces

I expect the following code to compile. Clang and VC++ both give me an error on the line with else.
void MyFunction(std::int32_t& error)
{
std::int32_t variable = 0;
if(GetSomething())
error = EOK;
else
error = ERROR;
}
If I put curly braces around error = EOK;, then it compiles. Why does VC++ say:
illegal else without matching if
?
My full code is below, replacing std::uint32_t with a typedef. It still gives the same error in VC++.
using sint32 = int;
#define ERROR 5;
#define EOK 0;
bool GetSomething();
void MyFunction(sint32& error)
{
sint32 variable = 0;
if (GetSomething())
error = EOK;
else
error = ERROR;
}
If your definition of EOK is as follows:
#define EOK 0;
then it would cause this type of error, because it forcibly terminates the if-statement before the else is reached, making it an else without a matching if. The compiler sees this code after macro replacement:
if(GetSomething())
error = 0;;
else
Here is a possible fix:
enum
{
EOK = 0,
ERROR = 5
};
Note that all identifiers starting with E followed by either another uppercase letter or a number are reserved for use as macro names by <cerrno>, so to avoid name clashes consider using a different naming convention for your errors.
To be implest and more efficient, you can do :
error = (GetSomething()) ? 0 : 5 ;
And if you want to with enum as Matt say , it become :
error = (GetSomething()) ? enum.EOK : enum.ERROR ;

unordered_map used incorrectly or a bug?

hey guys i need some help immediately...
i am usually using c# but have to make a code in c++ so was quickly going through the useful datatypes and procedures
Here's the code:
#include<iostream>
#include <unordered_map>
#include <vector>
#include <string>
using namespace std;
void insertInHashtable(string customerString,unordered_map<string, string> &hashtable )
{
string customerPurchaseArray, name;
int i= 0, firstCommaPosition = 0;
int length = customerString.length();
while (i<length)
if (customerString[i] == ',')
{
firstCommaPosition = i;
break;
}
else
i++;
customerPurchaseArray.assign(customerString, firstCommaPosition + 1, string::npos);
name.assign(customerString, 0, firstCommaPosition - 1);
hashtable.insert(name, customerPurchaseArray);
}
int main (int args[])
{
string value = " error...!!!";
unordered_map<string, string> hashtable;
string customerString = "Ayush,p1234,p345,p34,p43,p444";
insertInHashtable(customerString, hashtable);
unordered_map<string, string>::iterator got = hashtable.find("Ayush");
if (got != hashtable.end())
value = got->second;
std::cout<<value;
char ch;
std::cin>>ch;
}
when i got stuck at this issue..
here im trying to use unordered_map<string, string> but im getting a series of errors which i dont really get like :
Error 1 error C2039: 'iterator_category' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 373 1 CPPTP
and 5 others...
as i only got to know about these functions an hour ago im presuming that its just wrong usage or call by reference is not rite...
so does anyone have any idea what the problem is and how to solve it... any advice will be appreciated...
Use either:
hashtable.insert(make_pair(name, customerPurchaseArray));
Or:
hashtable.emplace(name, customerPurchaseArray);
Or:
hashtable[name] = customerPurchaseArray;
Note that there's a difference: The first two will not change any existing elements, whereas the last one always, unconditionally overwrites existing elements.

Getting Errors In C Key Press Checker Code

I am getting these errors when I run my code in Dev-C++ 4.9.9.2 and yes, I'm a beginner
My program is a basic test program designed to give an int value on Left Shifts' Key Press.
In function int main()':
Do' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
;' before '{' token
'While' undeclared (first use this function)
;' before "on"
And here's my code:
using namespace std;
int main()
{
int on;
on = 1;
Do
{
GetASyncKeyState(VK_LSHIFT int key);
cout << key << endl;
}
While on = 1;
return 0;
}
Do/While should be do/while (i.e. lower case). It is a good idea to learn about/read the error and warning messages generated by the compiler.

Invalid use of auto

In this code:
for ( ;(auto i = std::find(some_string.begin(),some_string.end(),'%')) != some_string.end();)
{
}
I'm getting error from gcc 4.7.1:
error: invalid use of 'auto'|
any ideas why? shouldn't that be correctly compiled?
I think it has nothing to do with auto. You just cannot declare variables in random places, for example this will not compile either - an equivalent of what you were trying to do, but without auto:
int main() {
for ( ; (int i = 0) != 1; ++i)
;
return 0;
}
If this is in a loop, you'll only ever find the first '%'. You need to resume searching from i, not some_string.begin() to find subsequent '%'.
auto i = std::find(some_string.begin(), some_string.end(), '%'));
while (i != some_string.end()) {
// Your code here.
i = std::find(i, some_string.end(), '%')); // Find next '%'.
}