Depth-first search algorithm implementation - c++

The following C++ Depth-first search program won't compile.
#include <iostream>
using namespace std;
class Stack
{
private:
const int size=20;
int *st;
int top;
public :
Stack(){
st =new int[size];
top=-1;
}
~Stack(){
delete[] st;
top=-1;
}
void push(int j){
st[++top]=j;
}
int pop(){
return st[top--];
}
int peek(){
return st[top];
}
bool empthy(){
return (top==-1);
}
};
class Vertex{
public:
char label;
bool visited;
public:
Vertex(){
}
Vertex(char lab){
label=lab;
visited=false;
}
};
class Graph{
private:
const int maxvertex=20;
Vertex* vertexlist;
int **adj;
int nverts;
Stack *stack;
public:
Graph(){
vertexlist=new Vertex[maxvertex];
adj=new int*[maxvertex];
for (int i=0;i<20;i++)
adj[i]=new int[maxvertex];
nverts=0;
for (int i=0;i<maxvertex;i++){
for (int j=0;j<maxvertex;j++){
adj[i][j]=0;
}
}
stack=new Stack();
}
void add(char lab){
vertexlist[nverts++]=new Vertex(lab);
}1
};
int main(){
return 0;
}
Here are the compilation errors I am getting:
> 6 IntelliSense: no operator "=" matches these
> operands c:\users\datuashvili\documents\visual studio
> 2010\projects\dfs\dfs\dfs.cpp 76 23 DFS 7 IntelliSense: expected a
> declaration c:\users\datuashvili\documents\visual studio
> 2010\projects\dfs\dfs\dfs.cpp 77 3 DFS Error 1 error C2864:
> 'Stack::size' : only static const integral data members can be
> initialized within a class c:\users\datuashvili\documents\visual
> studio 2010\projects\dfs\dfs\dfs.cpp 8 1 DFS Error 3 error C2864:
> 'Graph::maxvertex' : only static const integral data members can be
> initialized within a class c:\users\datuashvili\documents\visual
> studio 2010\projects\dfs\dfs\dfs.cpp 54 1 DFS Error 2 error C2758:
> 'Stack::size' : must be initialized in constructor base/member
> initializer list c:\users\datuashvili\documents\visual studio
> 2010\projects\dfs\dfs\dfs.cpp 12 1 DFS Error 4 error C2758:
> 'Graph::maxvertex' : must be initialized in constructor base/member
> initializer list c:\users\datuashvili\documents\visual studio
> 2010\projects\dfs\dfs\dfs.cpp 60 1 DFS Error 5 error C2679: binary '='
> : no operator found which takes a right-hand operand of type 'Vertex
> *' (or there is no acceptable conversion) c:\users\datuashvili\documents\visual studio
> 2010\projects\dfs\dfs\dfs.cpp 76 1 DFS

Change
const int size=20;
to
static const int size=20;
(static means it will be initialized once per-class, not per-object which would require an initialization list)
vertexlist[nverts++]=new Vertex(lab);
Your trying to set a Vertex to a Vertex*. This won't compile.

Related

error C2660: 'MouseListener::MousePressed' : function does not take 4 arguments

Im working on an c++ pseudo graphic button for a school assignment.
I use the observer pattern.
in Button.cpp:
void Button::notify()
{
for (int iterator = 0; iterator < listeners.size(); iterator++)
{
listeners[iterator]->MousePressed(*this, x, y, isLeft);
}
}
as you can see the MousePressed function receive 4 arguments but i get:
function does not take 4 arguments
in Button.h:
struct MouseListener
{
virtual void MousePressed(Button &b, int x, int y, bool isLeft) = 0;
};
class Button : public Label
{
public:
vector <MouseListener*> listeners;
.
.
.
in main:
struct MyListener : public MouseListener
{
MyListener(iControl &c) : _c(c) { }
void MousePressed(Button &b, int x, int y, bool isLeft)
{
_c.setForeground(Color::Red);
}
private:
iControl &_c;
};
int main(VOID)
{
errors:
Error 2 error C2061: syntax error : identifier 'Button' c:\users\gonen\unitedproj\unitedproj\Button.h 14 1 unitedProj
Error 4 error C2061: syntax error : identifier 'Button' c:\users\gonen\unitedproj\unitedproj\Button.h 14 1 unitedProj
Error 5 error C2660: 'MouseListener::MousePressed' : function does not take 4 arguments C:\Users\Gonen\unitedProj\unitedProj\Button.cpp 24 1 unitedProj
when the first two are for the declartion in main and the last is in the use of the function in Button.cpp.
help? when im standing with the mouse curser on the function in Button.cpp i get:
void MouseListener::MousePressed(Button &b,int x,int y, bool isLeft)
I just need this to work so i can move on and let the rest of the group members to use the button in the controls they implementing... and move on to the next i need to do :-(
edit: thank you for the fast answer.
I tried to do what you asked. now i get:
*Error 3 error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup C:\Users\Gonen\unitedProj\unitedProj\MSVCRTD.lib(crtexe.obj) un‌​itedProj
Button is not declared at the point where MousePressed is declared, so it cannot be used. I suggest you should use forward declaration.
class Button; // add this line
struct MouseListener
{
virtual void MousePressed(Button &b, int x, int y, bool isLeft) = 0;
};
class Button : public Label
{
public:
vector <MouseListener*> listeners;
.
.
.

inherit constructors from vector

In Stroustrup, C++ Programming Language 4th Edition, he wrote the on page 79 the following code:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <allocators>
using std::vector;
using namespace std;
template<class T>
class Vector : vector<T>
{
private:
T elem[50];
int size;
public:
***using vector<T>::vector; // inherit constructors***
T& operator[](size_type i) { check(i); return this−>elem(i); }
const T& operator=(size_type i) const {
check(i);
return this−>elem(i);
}
void check(size_type i) { if (this−>size()<i) throw Bad_index(i); }
};
int _tmain(int argc, _TCHAR* argv[])
{
Vector <int> V{ 1, 2, 3, 4 };
return 0;
}
When I compile the program, I receive the following errors:
Error 3 error C2440: 'initializing' : cannot convert from
'initializer-list' to 'Vector' c:\computer programming\c++
programming
language\code\ch3\consoleapplication3\consoleapplication3\consoleapplication3.cpp 28 1 ConsoleApplication3
Error 1 error C2886: 'vector>' : symbol cannot
be used in a member using-declaration c:\computer programming\c++
programming
language\code\ch3\consoleapplication3\consoleapplication3\consoleapplication3.cpp 17 1 ConsoleApplication3
Error 2 error C2886: 'vector>' : symbol cannot
be used in a member using-declaration c:\computer programming\c++
programming
language\code\ch3\consoleapplication3\consoleapplication3\consoleapplication3.cpp 17 1 ConsoleApplication3
4 IntelliSense: no instance of constructor "Vector::Vector [with
T=int]" matches the argument list
argument types are: (int, int, int, int) c:\Computer Programming\C++ Programming
Language\Code\Ch3\ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.cpp 28 16 ConsoleApplication3
My question involves mostly Error 2 error C2886: which refers to the using directive. Visual Basic defines the error as follow:
'class::identifier' : symbol cannot be used in a member using-declaration
A using declaration uses a symbol, such as a namespace name. A using declaration is for declaring base class members.
Stroustrup apparently uses it but I have failed to duplicate his method. Is there a header I am missing or what? Could someone explain my error? thank you jmg.

C++ Forward Declaration Error using Struct in Class as well as Constructor and Header

I have posted this already one day ago but I did not know how to add a second question to my first question.
I get a forward declaration error. You told me that it should be no problem if I define my class in KdTree.h and my functions, structs, etc in KdTree.cpp. However it does not work so here I post my whole code:
This is my header:
#Includes <iostream>
#Includes others
using namespace TooN;
#ifndef KDTREE_H_
#define KDTREE_H_
class KdTree {
public:
KdTree(std::vector<TooN::Vector<3,GLfloat> > & ,size_t);
struct node;
struct temptask;
struct temphold;
struct ...;
double function(...);
...;
std::vector<node> nodes;
std::vector < int > searchInRadius(const TooN::Vector<3, GLfloat> &,float , const std::vector<TooN::Vector<3,GLfloat> > & );
};
#endif
So and this is my KdTree.cpp:
#include "KdTree.h"
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
const size_t stacksize = 200;
nodes.push_back(node());
temphold tasksarray[stacksize] = {0,pointssize-1,0,0};
int taskindex = 0;
...A lot more stuff
if (!is_leaf(n)){
do something;
}
}
And then my functions in KdTree.cpp
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
And here my first 3 Compiler messages:( :
jni/Visual/KdTree.cpp: In constructor 'KdTree::KdTree(const std::vector<TooN::Vector<3, float> >&, size_t)':
jni/Visual/KdTree.cpp:32:23: error: invalid use of incomplete type 'struct KdTree::node'
nodes.push_back(node());
^
In file included from jni/Visual/KdTree.cpp:8:0:
jni/Visual/KdTree.h:29:9: error: forward declaration of 'struct KdTree::node'
struct node;
^
jni/Visual/KdTree.cpp:33:54: error: elements of array 'KdTree::temphold tasksarray [200]' have incomplete type
temphold tasksarray[stacksize] = {0,pointssize-1,0,0}; //starting at firstpoint = 0 index, lastpoint = lastindex, nodenumber = 0 index, dim = x-dimension (i.e. 0)
And a lot more of these kind of messages.
The struct definitions must still occur before their first use in the .cpp file.
#include "KdTree.h"
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
...A lot more stuff
}
Put the implementation of node at the beginning of KdTree.cpp, or at least before its first use:
#include "KdTree.h"
struct KdTree::node {
// ...
};
KdTree::KdTree() {
// ...
}

Making Priority Queue of Objects

I have error in line 7 that missing ; before * I want to make Priority Queue of Objects which gets priority by Student ID and also it is not necessary to have object pointer it can be object itself
#include<iostream>
#include<string>
using namespace std;
struct node
{
int priority;
Student * S;
node * next;
};
class Student
{
int ID;
string name;
public:
Student()
{
cin>>ID;
cin>>name;
}
void out()
{
cout<<"ID is : "<<ID<<" "<<"Name is : "<<name<<endl;
}
};
class Priority_Queue
{
node * head;
//node * back;
public:
Priority_Queue()
{
head=NULL;
//back=NULL;
}
void push(Student * Q, int a)
{
node * p=new node;
p->next=NULL;
p->priority=a;
p->S=Q;
if(head==NULL)
head=p;
else
{
node * q=head;
node * r=NULL;
while(a<=q->priority)
{
r=q;
q=q->next;
}
r->next=p;
p->next=q;
}
}
Student * pop()
{
if(isempty())
{
cout<<"Empty"<<endl;
exit(1);
}
else
{
return head->S;
head =head->next;
}
}
bool isempty()
{
if(head==NULL)
return true;
else return false;
}
};
int main()
{
Student S1,S2,S3,S4;
return 0;
}
Errors in my Code
1>d:\codes\priority queue\priority queue\1.cpp(7): error C2143: syntax error : missing ';' before '*'
1>d:\codes\priority queue\priority queue\1.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\codes\priority queue\priority queue\1.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\codes\priority queue\priority queue\1.cpp(41): error C2039: 'S' : is not a member of 'node'
1> d:\codes\priority queue\priority queue\1.cpp(5) : see declaration of 'node'
1>d:\codes\priority queue\priority queue\1.cpp(66): error C2039: 'S' : is not a member of 'node'
1> d:\codes\priority queue\priority queue\1.cpp(5) : see declaration of 'node'
Actually the problem is, that the struct node does not know about the class student, as it is defined after. A workaround is to declare Student before node, but you could aswell put Student in an extra header and include that header in the node header (personally I'd prefer that way.).
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
You should use forward declaration:
struct node;
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
// . . .

How to fix error C2511: overloaded member function not found in C++?

I have a problem to pass my own class as a parameter.
Here is my codes:
PayloadContainer.h
namespace Project
{
namespace C
{
namespace Helper
{
class PayloadItem
{
public:
string Key;
string Value;
char Type;
char Mode;
char IsArray;
int FieldCount;
char FieldType;
int RowCount;
};
class PayloadContainer
{
public:
PayloadContainer( const char *Command );
PayloadContainer(void);
~PayloadContainer(void);
public:
vector<PayloadItem> PayloadItems;
};
}
}
}
ParseBinary.h
namespace Project
{
namespace C
{
namespace Helper
{
class ParseBinary
{
public:
ParseBinary(void);
~ParseBinary(void);
private:
void WriteRequestToBinary( const char *BinFileName );
void WriteRequestRecord( unsigned char file[], PayloadItem& item, string charSet );
};
}
}
}
ParseBinary.cpp
namespace Project
{
namespace C
{
namespace Helper
{
void ParseBinary::WriteRequestToBinary( const char *BinFileName )
{
unsigned char in;
// Do Something
for (auto &item : _payload->PayloadItems)
{
// Do Something
WriteRequestRecord( in, (Helper::PayloadItem)item, _payload->CharSet );
}
}
void ParseBinary::WriteRequestRecord( unsigned char file[], PayloadItem& item, string charSet )
{
-----> here, I get "error C2511: overloaded member function not found".
}
}
}
}
What I am trying to do is iterating vector where PayloadItem is my own class, and pass that class to a function.
But, when I build it I get this error C2511: overloaded member function not found error.
Please tell me where to fix this.
EDIT
This is the error message.
Error 9 error C2061: syntax error : identifier 'PayloadItem' c:\webdev\Project.c.helper\ParseBinary.h 46 1 Project.C.Helper
error C2511: overloaded member function not found in 'Project::C::Helper::ParseBinary' c:\webdev\Project.c.helper\ParseBinary.cpp 958 1 Project.C.Helper