reference to 'list' is ambiguous including header file - c++

When I add 'bits/stdc++.h'header file, it shows [Error] reference to 'list' is ambiguous. But when I erase the header and remain all other headers, the code is compiling properly.
#include <bits/stdc++.h>
#include<stdio.h>
#include <iostream>
#include<stdlib.h>
#include <string.h>
#include <sstream>
#define LIST_INIT_SIZE 2
#define NULL_VALUE -99999
#define SUCCESS_VALUE 99999
using namespace std;
int listMaxSize;
int * list;
int length;
void initializeList()
{
listMaxSize = LIST_INIT_SIZE;
list = (int*)malloc(sizeof(int)*listMaxSize) ;
length = 0 ;
}

Thats because the header <bits/stdc++.h> has its own version of list i.e. from the list template <list>.
Having two copies or different definitions of the same would result in ambiguity.
If its working without the bits header (STL) then simply dont use it.

Related

int Method in C++ Class "Requies and Identifier"

I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/

N does not name a type

// q.h file
#ifndef __Q_H__
#define __Q_H__
using namespace std;
#include "n.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
class Q
{
public:
Q();
private:
N* beginning; //error N does not name a type
N* end; //error N does not name a type
int count;
};
#endif // end of file
// q.cpp file
#include "q.h"
#include "n.h"
#include <iostream>
using namespace std;
#include<string>
Q::Q()
{
beginning = NULL;
end = NULL;
}
// n.h file
#ifndef _N_H__
#define _N_H__
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "q.h"
class N
{
public:
N(int);
// next is a pointer object of type N
N* next;
// memeber for Node class
int value;
};
#endif
// n.cpp file
#include<string>
#include "q.h"
#include "n.h"
N::N(int v)
{
value = v;
}
I get an error at the following lines below. I tried changing user namespace std; to the first line of each file but that still does not work. I've also tried changing the order of q.h and n.h in which they are presented but still nothing.
N* beginning;
N* end;
Also I know one great solution is by using "forward declare" but this is part of a test file that i given to me by someone so I cannot do a forward declaration of a class.
So if anyone can please just take a look and see how I can fix this.
Thanks.
You have a circular reference (q.h includes n.h and n.h includes q.h). This error occurs because the compiler is including (maybe because the main program) q.h before than n.h. Thus, q.h need to include an empty class declaration as follows to Q knows about N.
...
#include <string>
class N;
class Q
{
...
If you can't edit those files, you still can add the empty class declaration anywhere before the "Q" class declaration (e.g. before including q.h in your main program). However, the class N don't need anything from Q, so you don't need to include q.h in n.h.
Since #include's are done in a way, where the preprocessor just takes the file that you are #include'ing and copy-pastes in the spot of #include statement, when n.h is being processed, it includes q.h, and then class Q appears before class N. Hence, it doesn't know what the class N is, since the compiler works in linear fashion (from the start of the file to the end).
So, it is my suggestion, to remove #include "q.h" statement from n.h, since the class N doesn't use class Q.

C++: Accessing data of dynamic structures within extern function

I have a problem with passing dynamically allocated structures to a function and accessing it's content.
The program uses mex to pass data from Matlab to C++. I use Visual Studio.
The structure I define in a header in 'InOut.h'
#include <string>
#include <cstdint>
#include <cstdlib>
struct sObjects {
std::string Type;
float *Position;
};
typedef struct sObject sObject;
In the main function I than allocate the structure is in 'MainFcn_Mex.cpp'
#include "MainFcn_Mex.h"
// ...
// get number of Objects from Matlab
int N_Obj = mxGetNumberOfElements(prhs[1]);
sObjects *Objects = new sObjects[N_Obj];
for (int k=0; k<N_Obj; k++) {
// get the pointer pointer map
pMap = mxGetField(prhs[1],k,"Type");
Objects[k].Type = mxArrayToString(pMap);
// get the pointer pointer map
pMap = mxGetField(prhs[1],k,"Position");
// setting pointer to the first Element
Objects[k].Position = (float*)mxGetPr(pMap);
mexPrintf("Objects(%d,1).Type: %s \n", k+1, Objects[k].Type);
}
create_Objects(Objects, N_Obj);
The function create_Objects is in a differente file 'create_Objects.cpp' and included via 'MainFcn_Mex.h':
#include <stdio.h>
#include <direct.h>
#define _USE_MATH_DEFINES
#include "math.h"
#include <cmath>
#include "mex.h"
#include "matrix.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include "InOut.h"
void create_Objects(sObjects *Objects, int N_Obj);
The content of 'create_Objects.cpp' so far is:
#define _USE_MATH_DEFINES
#include "math.h"
#include <cmath>
#include "InOut.h"
#include "mex.h"
void create_Objects(sObjects *Objects, int N_Obj)
{
for (int k=0; k<N_Obj; k++) {
mexPrintf("Objects(%d,1).Type: %s \n", k+1, Objects[k].Type);
}
}
Visual Studio tells me:
"error C2676: binary '[' : 'sObjects' does not define this operator or
a conversion to a type acceptable to the predefined operator"
Why can I access the data in the main function and not in seccondary functions?
How can I access a dynamically allocated structure in other functions, when its size isn't known at compile time?
Thanks a lot for your time!
It looks like you are trying to use struct directly as a typedef. Simply add typedef to your struct definition, will turn it into a type.
Like this:
... #include "mex.h"
typedef struct sObjects {
std::string Type;
float *Position;
};
(Otherwise you should use the full struct keyword as in void create_OpticsObjects(struct sObjects &Objects, int N_Obj).)
Your function prototypes don't need the extern qualifier.
You don't need extern unless you want globals variables. You seem to want simply global structs or types in your example, so extern is not required.
Using extern for globals
What you could be referring to is an instance of your object (or a pointer to your object), and that can be made global by using extern. as in this excerpt from the header file:
... #include "mex.h"
typedef struct sObjects {
std::string Type;
float *Position;
};
extern sObjects *pointerToOnesObjects;
Then in ONE source file, you need to declare the 'real' variable as in (this is good to initialise it here):
sObjects *pointerToOnesObjects = NULL;
With this method your variable pointerToOnesObjects is now available globally (in all your source files that use the same header file).

myvector does not name a type

main.cpp
#include <vector>
#include <iostream>
#include "normal.h"
using namespace std;
int main()
{
return 0;
}
normal.h
#ifndef NORMAL_H
#define NORMAL_H
#include <vector>
#include <iostream>
using namespace std;
vector < int > myvector;
myvector.push_back(12);//does not name a type
#endif
I know I need to somehow include vector<int> myvector in main.cpp but can't figure the way. I've looked at my previous programs and didn't need to include anything in main.cpp.
The problem is that the code
myvector.push_back(12); is not inside any function. Outside of functions you may only declare (and possibly initialize) variables, you cannot put other code.
So, even though you can declare your vector in the .h file (probably to have it available in many files) you should move this line inside the main() or some other function.

How to declare vectors in C++?

I'm trying to use a vector of strings in my code instead of an array of strings but apparently I miss some detail in the declaration of the vector. Using the following code, I get this error: ‘vector’ was not declared in this scope
// Try to implement a vector of string elements
#include<iostream>
using namespace std;
int main() {
const int MAX_ITEMS = 10;
vector<string> my_vector(MAX_ITEMS);
return 0;
}
How should I correctly declare the vector?
You should add these includes:
#include <vector>
#include <string>
You have to include the header:
#include <vector>
#include <string>
You need:
#include <vector>