Using STL's list object for Stack creation - c++

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);
};

Related

Object in Node Linked List

I am trying to create a inventory linked list where the user can add a product(id,info,price,count) and then to store the object in a linked list.
The issue I am having is with the Node class giving error "missing type specifier - int assumed. Note: C++ does not support default-int" and so on for each statement in the Node Class.
#ifndef INVENTORY_H
#define INVENTORY_H
#include<iostream>
#include <string>
#include <iomanip>
using namespace std;
class Node
{
public:
Node(){}
Node(const Inventory& theData, Node *theLink)
: data(theData), link(theLink){}
Node* getLink() const { return link; }
Inventory getData() const { return data; }
void setData(const Inventory& theData)
{
data = theData;
}
void setLink(Node *theLink) { link = theLink; }
private:
Inventory data;
Node *link; //pointer that points to next node
};
class Inventory
{
public:
Inventory();
void addPart(int, string, int, int);
void findPart(int);
void toBinary();
void quit();
private:
int id;
int price;
string partInfo;
int partCount;
Node* first;
Node* last;
int count;
};
#endif
And the errors are:
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2143: syntax error : missing ',' before '&'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2146: syntax error : missing ';' before identifier 'getData'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): warning C4183: 'getData': missing return type; assumed to be a member function returning 'int'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C2143: syntax error : missing ',' before '&'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C2146: syntax error : missing ';' before identifier 'data'
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'Thedata' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'theLink' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2614: 'Node' : illegal member initialization: 'data' is not a base or member
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2065: 'data' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'data' : undeclared identifier
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'theData' : undeclared identifier
You're just looking at a standard case of the compiler not knowing what your custom types are because of the order you have defined them. Even if you forward declare a class (put "class Inventory;" somewhere up above), you can't use it for certain things. Declaring a member variable that is not a pointer is one of them.
However, if you invert the definitions (Inventory before Node) and forward declare Node ("class Node;"), your code will compile because you only have Node* in your Inventory class.
Check this answer for more details: https://stackoverflow.com/a/553869/128581
As pointed out, this is not the best design if this code is not strictly for learning. STL containers cover most common data structures (doubly linked and singly linked lists are two of them). stl::list and stl::forward_list respectively.
The C++ compiler reads the source code from top to bottom. When it gets to line 13, it hasn’t heard of an Inventory type. It thinks Inventory must therefore be the parameter name, and gets really confused.
The solution to this is to switch the order of the Node and Inventory classes, and to pre-declare the Node class before the start of the Inventory class, by inserting the following line
class Node;
before the start of class Inventory.
Switching the order has Inventory defined before Node, which is important because the compiler needs to know everything about an Inventory to construct a Node, since Nodes contain Inventorys. The extra line tells the compiler that there is a Node class, but not anything about what it is; this way you can use pointers to Nodes (or anything else which doesn’t require knowing the layout of a Node), but can’t do anything else with them until they’re fully defined. Since Inventory only uses pointers, this shouldn’t be a problem.
However, as described I don’t see why Inventory needs the node pointers at all; it seems like your Inventory class is what you called a product in your English description, and a product shouldn’t need to know about the rest of the data. You also might want to just use std::forward_list instead of trying to implement your own linked list type.

Compilation error upgrading from VS 2008 to VS 2015

I get errors compiling the code below when ugrading from VS 2008 to VS2015. The code is from the com4j project. Help wanted. Thanks!
syntax error: missing ';' before '<' missing type specifier
int assumed. Note: C++ does not support default-int
'array': ambiguous symbol
unexpected token(s) preceding ';'
Code:
// Class to marshal SAFEARRAY to Java multi dimensional array
//
// itemType : array item type
// XDUCER : converter for each array item
template < VARTYPE itemType, class XDUCER >
class ToJavaMultiDimlArrayMarshaller {
typedef array::Array<typename XDUCER::JavaType> JARRAY; // Errors here
typedef SAFEARRAY* NativeType;
typedef jarray JavaType;
What does VARTYPE stand for? Is it a macro? Replacing with class or typename may help

Passing template class instance to a constructor of another class

My code:
BlockyWorld.hpp
#ifndef BLOCKYWORLD_H
#define BLOCKYWORLD_H
#include <CImg.h>
namespace logic {
class BlockyWorld {
public:
BlockyWorld( const CImg<float>* heightmap );
};
}
#endif // BLOCKYWORLD_H
BlockyWorld.cpp
#include "BlockyWorld.hpp"
namespace logic {
BlockyWorld::BlockyWorld( const CImg<float>* heightmap ) {}
}
main.cpp
#include <CImg.h>
#include "logic/BlockyWorld.hpp"
//...
CImg<float> heigthMap;
logic::BlockyWorld world( &heigthMap );
//...
I get alot of errors while compiling:
main.cpp:
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
main.cpp(85): error C2664: 'logic::BlockyWorld::BlockyWorld(const logic::BlockyWorld &)' : cannot convert argument 1 from 'cimg_library::CImg<float>' to 'const int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
BlockyWorld.hpp & cpp
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
include\logic\blockyworld.cpp(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.cpp(4): error C2143: syntax error : missing ',' before '<'
I don't think it's a circular inclusion error which sometimes causes these kinds of errors for me=).
I must be defining constructor wrong or maybe I'm defining implementation wrong? Was searching for an answer for abount an hour now so I would really use an explanation now.
And just to clarify - I'm not a beginner c/c++ programmer but these templates are confusing :(
Have a nice day and thank your for your answers.
CImg appears to be part of the cimg_library namespace.
Either add using namespace cimg_library to the top of your BlockyWorld.hpp file, or change the function signature to use the namespace like so:
BlockyWorld( const cimg_library::CImg<float>* heightmap );
Along with πάντα ῥεῖ's suggestion of matching up your pointer and reference types.

c++ weird struct and bitset error

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.

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).