Undeclared Identifier with all the right includes - c++

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.

Related

Execute Microsoft Visual Studio project multiple times with different parameters

I would ask for your help and apologise if the question doesn't make sense.
I have a Microsoft Visual Studio project which I want to execute it multiple times in one go, and every time I will change one parameter.
Please see below the concept:
#include <iostream>
using namespace std;
int size_list = 2;
int my_list[2] = { 5, 6 };
int main()
{
for (int i = 0; i < size_list; i++)
{
cout<<"the number is "<<my_list[i]<<endl;
}
return 0;
}
So, I would like to replace the loop and instead I will have each element of my_list as parameters.
Is there any way to do so?
Thanks
You can pass the parameters in main().
int main(int argc, char* argv[]){std::cout<< argv[1];}
And use script like
for ($i=0;$i -lt 5; $i++)
{
#your CPP project and parameters array
Start-Process test.exe $arr[0]
Wait-Process test
}

why my code is giving undeclared variable error?

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.

C++ Program Crash on exit

I have a very simple program that I don't know why a crash occurs on exit.
My main function is a simple cout<<"Hello world";
But I've added another cpp file:
system_information.cpp:
system_information::system_information() {
threadid_processid_map[22]=23;
processor_processid_map[128] = { 0L };
processor_threadid_map[128] = { 0L };
}
And the header file looks like:
system_information.h
class system_information{
public:
DWORD processor_processid_map[128];
DWORD processor_threadid_map[128];
unordered_map<DWORD, DWORD> threadid_processid_map;
system_information();
};
And I simply have this file to declare in instance of my class:
parse.cpp:
#include "system_information.h"
system_information sys_info;
My program crashes on exit on crt0at.c at this line:
onexitbegin_new = (_PVFV *) DecodePointer(__onexitbegin);
What am I doing wrong?
I'd say you're slightly confused about what:
processor_processid_map[128] = { 0L };
is actually doing. The fact that you have braces in there seems to indicate you think it will set all values in the array to zero but that is not the case. You can initialise the array that way but assigning to it is a different matter.
What that code is doing is trying to set element number 128 to zero and, since elements are restricted to the range 0..127, what you have there is undefined behaviour.
If you do wish to zero out the entire array, there are a number of ways to do this, including:
// Simple loop.
for (int i = 0; i < sizeof(id_map) / sizeof(*id_map); ++i)
id_map[i] = 0;
// Setting memory.
#include <cstring>
memset(id_map, 0, sizeof(id_map));
// Fill algorithm.
#include <algorithm>
std::fill(id_map, id_map + sizeof(id_map) / sizeof(*id_map), 0);

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 compilation error using Map in C++ STL?

Using MAP in the below program generate some compiler errors . Not getting what they mean.
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
char maze [61][61], q;
int n , m , i , j , x , y;
map < char , char > left ;
map < char , char > right ;
char orient ;
int main(){
left ['N'] = 'W' ;
left ['E'] = 'S';
left['S'] = 'E';
left['W'] = 'N';
right['N'] = 'E';
right['E'] = 'S';
right['S'] = 'W';
right['W'] = 'N';
scanf( "%d %d" , &n , &m) ;
for ( i = 0 ; i < n ; i++)
scanf("%s", maze[i]);
scanf("%d %d" , &x ,&y);
orient = 'N' ;
x = x - 1 ; y = y - 1 ;
return 0 ;
}
Getting compiltaion errors like :
prog.cpp: In function ‘int main()’:
prog.cpp:15:1: error: reference to ‘left’ is ambiguous
left['N'] = 'W';
prog.cpp:9:21: note: candidates are: std::map<char, char> left
map < char , char > left ;
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from prog.cpp:1: /usr/include/c++/4.8/bits/ios_base.h:916:3:
note: std::ios_base& std::left(std::ios_base&)
left(ios_base& __base)
What is the mistake can you please point out and what does it mean ??
For more details :
http://ideone.com/CqBiS0
Functions with name left and right already exists in ios header. They are IO manipulators.
So, this is why you should use explicit namespace resolution instead of just writing using namespace std. And, of course, you should avoid using global variables.
In the iostream header, there's a symbol named left. Luckily, it's under the std namespace. Unfortunately, you are using namespace std, so both your map and the symbol from the iostream header are now referenced by "left" - which creates this ambiguity.
To resolve this, either:
1. Rename your map, or
2. Remove the "using namespace std" statement, and use the prefix std:: where it's required.
Here's your culprit:
using namespace std;
This drags the standard library's std::left into the global namespace, where it conflicts with your own left in the same namespace. Get rid of it, and add std:: to any names from the standard library you're using. Dumping the entirety of a large namespace into the global namespace is a recipe for name conflicts like this one.
Also, you probably don't want to use global variables; they are fraught with more perils than just namespace pollution. Moving the variables inside main will also fix this particular error. I suggest you do both, and make both habits.