c++ weird struct and bitset error - c++

I have this inside my private class declarations
#include "stdafx.h"
using namespace std;
template <typename Key, typename T>
class A{
//....
private:
static const unsigned int HSIZE = 32;
struct Bucket {
Key key;
T value;
bitset<HSIZE> jumpMap;
};
//....
};
Gives the following errors:
Error 1 error C4430: missing type specifier - int assumed
Error 2 error C2059: syntax error : '<'
Error 3 error C2238: unexpected token(s) preceding ';'
And when i remove the bitset line, it gives me no errors. What am i doing wrong?
EDIT: Added more relevant lines

Have you included the bitset header? I think you have missed it?

Should HMAX be HSIZE instead? Otherwise make sure you include < bitset >, and that the name is in scope. You probably have a using namespace std in your code, since you don't qualify it with std::. But my bet goes to HMAX <-> HSIZE.

Related

first time using .h and multiple .cpp files in one project, errorssss :(

second post on StackOverflow. I just have some general questions as to why my program is acting the way it is, I don't want help in completing it I was just absent from class on Friday and apparently I missed a lot.
I'm tasked to design a program that contains 3 .cpp and 2 .h files, in essence it will search and sort through arrays of strings using the bubble sort, insertion sort, selection sort methods and sequential and binary search. We are then supposed to benchmark each method to figure out which is the fastest.
I am just confused as to why the compiler keeps yelling at me, it's not making much sense I've been sitting here for about an hour fiddling around with different options or typing the code in differently but to no avail.
My header file
const int NOT_FOUND = -1;
int sequentialSearch(string a[], string needle, int length );
JohnSearch.cpp
#include "JohnSearch.h"
#include <string>
int sequentialSearch(string copied[], string needle, int length)
{
int i; // iteration variable
// look at every element to see if it is the same as needle
for( i = 0; i < length; i++ )
if( copied[i] == needle )
return i;
return NOT_FOUND;
}
TestSearch.cpp
#include "JohnSearch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
** printArray(title,a,length): print out title and then the contents of array a
*/
void printArray(string title, string ref[], int length )
{
int i; // array iteration
cout << title << ": \n";
for( i = 0; i < length; i++ )
cout<<setw(20)<<ref[i]<<"\n";
}
int main(void)
{
string reference[]={"John", "Allen", "Kevin", "Elisabeth"};
const int SZ=sizeof(reference)/sizeof(reference[0]);
string copied[SZ];
printArray("Reference", reference, SZ);
// sequential search (on unsorted array)
cout<<"Search.sequential(ref,Kevin):\t"<<sequentialSearch(reference, "Kevin", SZ)<<endl;
system("Pause");
return 0;
}
Errors
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
testjohnsearch.cpp(28): error C3861: 'copyArray': identifier not found
testjohnsearch.cpp(31): error C2064: term does not evaluate to a function taking 3 arguments
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
johnsearch.cpp(7): error C2065: 'string' : undeclared identifier
johnsearch.cpp(7): error C2146: syntax error : missing ')' before identifier 'copied'
johnsearch.cpp(7): error C2374: 'sequentialSearch' : redefinition; multiple initialization
johnsearch.h(2) : see declaration of 'sequentialSearch'
johnsearch.cpp(7): error C2059: syntax error : ')'
johnsearch.cpp(8): error C2143: syntax error : missing ';' before '{'
johnsearch.cpp(8): error C2447: '{' : missing function header (old-style formal list?)
I'm obviously doing something completely and utterly wrong. I need JohnSearch.cpp for JohnSearch.h right? The forward declaration of the function in JohnSearch.h is defined in JohnSearch.cpp so I need those two files correct?
I'm just really confused. The example program we are supposed to modify has 2 .h files and 3 .cpp files. 2 of those .cpp files correspond with the 2 .h files so thats why I assumed I would also need 2 .h files and 3 .cpp files.
String is still undefined.
johnsearch.h(2): error C2065: 'string' : undeclared identifier
Your header file uses string , so you'll need to include <string>, before your declarations. You also need to qualify it as std::string since the string class resides in the std namespace
So your header file becomes:
#include <string>
const int NOT_FOUND = -1;
int sequentialSearch(std::string a[], std::string needle, int length );
(you should also use include guards in your header files)
Your JohnSearch.cpp also uses string, again, since string is in the std namespace, you'll get errors if you don't use std::string
In your TestSearch.cpp, you have a using namespace std; at the top, you could do the same in JohnSearch.cpp too, that way you can use string instead of std::string
When in doubt, simplify. You can boil the code down to something like this:
#include "JohnSearch.h"
void sequentialSearch(string needle)
{
}
and get the same error (and maybe a warning about an unused parameter).
Yes, string is a type of variable, but it's not innate in the C++ language itself, it's in one of the standard libraries, something you have to tell the compiler about:
#include "JohnSearch.h"
#include <string>
using std::string;
void sequentialSearch(string needle)
{
}
In your header file that you include, you need to have the exact same signature than your function in the cpp file.
Also dont forget to #include <string>, and then use a string like : std::string
E.g.
Int function(int number, int number2);
And in your cpp
Int function(int number, int number2)
{
// code
}
Signature is "int function(int, int)".

A dll with a vector of vectors. in one of its classes methods

In my c++ program i'm trying to create a dll that houses the functionality of my a* algorithm.
I encounter a problem when trying to pass the map into it, I first tried to use a 2d array, but that limited my map sizes, so i'm now trying to use a vector in a vector and I keep hitting some odd snag.
In my dlls .h file:
namespace IInterface
{
class IInterface
{
public:
// Sets the map
static __declspec(dllexport) void setMap(int h, int w,vector<vector<byte>> &myarray);
private:
static vector<vector<byte>> mymap;
}
Finaly in the .cpp i have:
#include "IInterface.h"
#include <Windows.h>
#include <stdexcept>
#include <vector>
using namespace std;
namespace IInterface
{
void IInterface::setMap(int h, int w,vector<vector<byte>> &myarray)
{
mymap = myarray;
}
}
Im getting a few errors on compilation even tho the code looks fine to me.
error C2061: syntax error : identifier 'vector' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 7 1 DMAstarDLL
error C2143: syntax error : missing ';' before '<' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2238: unexpected token(s) preceding ';' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
error C2511: 'void IInterface::IInterface::setMap(int,int,std::vector<_Ty> &)' : overloaded member function not found in 'IInterface::IInterface' c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.cpp 13 1 DMAstarDLL
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\steven\documents\github\schooladvgdproject\game code\deathastardll\iinterface.h 21 1 DMAstarDLL
I looked at some samples, but there was really nothing that matched this scenario. I have a sneaking suspicion i'm forgetting something crucial, but I cant see it. Ideas on getting this to work?
your dlls.h does not include vector type - you should tell the compiler vector definition and include .
Tip: don't use using namespace std; in header file only in cpp. Instead of this use std::vector ...etc.
Secondly, be careful when your dll interface contains stl. This library differs as regards Release and Debug versions, so if you load Release dll in Debug program you could have problems.

Using STL's list object for Stack creation

I want to create a list of stacks in C++ but the compiler gives me some error messages:
#include <list>
#include <stack>
class cName
{
[...]
list<stack> list_stack;
[...]
}
Errors:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
std::stack is a template, you need to use it with template arguments. For sample:
class cName
{
typedef int ConcreteType;
std::list<stack<ConcreteType> > list_stack;
^^^^ use it with real type
};
Stacks are also templated, so it should be
list<stack <...> > list_stack;
If you want your stack to handle just one type, for example int, change stack in your code to int:
list<int> list_stack;
Otherwise you should create your own template type instead of using stack:
template <class T>
class List
{
list<T> list_stack;
T top();
void push(T v);
};

Errors when trying to using vector

At the top of my file main.h I have:
#include <vector>
class Blah
{
public:
Blah(){}
~Blah(){}
protected:
vector<int> someVector;
public:
//methods
};
When I try to compile, the vector declaration line gives the errors:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
I can't figure out what's causing this. Anybody see what I'm doing wrong?
The vector class is part of the std namespace. You need to replace your declaration with std::vector<int> instead.
It's in the std namespace:
std::vector<int> someVector;
vector is part of the std namespace and so you need to add std:: to your declaration:
std::vector<int> someVector;
Since the suggestion was made in another answers, I want to also discourage the use of using namespace std since it is considered bad practice
Instead of using,
std::vector someVector;
Always try to use,
using namespace std;
Because it will help you not to type 'std::' again and again, and it is not considered a good practice.

How does a vector<T> get resolved as a function parameter for a templated function?

I have a helper function that creates an Array_Ref obj. The function has a parameter, vector<t> - that the compiler is complaining about. I'm using VS2010.
I put the function in a .h by itself.
I put the function in Array_Ref.h
I put it in a .cpp file.
I put typename in front of vector<T>
I put typedef typename in front of vector<T>
Nothing seems to work.
#include <vector>
template<class T>
Array_Ref<T> make_ref(vector<T> &v, int s)
{
return (v.size()) ? Array_Ref<T>(v,s): Array_Ref<T>(0,0);
}
I'm getting:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed.
error C2988: unrecognizable template declaration/definition
error C2059: syntax error : '<'
However, putting this in the same header file as Array_Ref.h works just fine:
template<class T,int size>
Array_Ref<T> make_ref(T (&p)[size])
{
return (p) ? Array_Ref<T>(p,size): Array_Ref<T>(0,0);
}
It's std::vector, not vector. Also, you don't appear to have defined Array_Ref anywhere.
Perhaps the std namespace is missing ? Change vector to std::vector (avoid using namespace directives in header files).