Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I searched a lot to clear the following error, couldn't find the answer.
I am getting the following errors. pls someone help me thanks in advance
ERROR: unable to open vector headerfile,
";" expected
#include <iostream.h>
#include <vector>
template <typename T>
class MyQueue
{
std::vector<T> data;
public:
void Add(T const &);
void Remove();
void Print();
};
template <typename T> void MyQueue<T> ::Add(T const &d)
{
data.push_back(d);
}
template <typename T> void MyQueue<T>::Remove()
{
data.erase(data.begin( ) + 0,data.begin( ) + 1);
}
template <typename T> void MyQueue<T>::Print()
{
std::vector <int>::iterator It1;
It1 = data.begin();
for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
cout << " " << *It1<<endl;
}
//Usage for C++ class templates
void main()
{
MyQueue<int> q;
q.Add(1);
q.Add(2);
cout<<"Before removing data"<<endl;
q.Print();
q.Remove();
cout<<"After removing data"<<endl;
q.Print();
}
change
#include <iostream.h>
to
#include <iostream>
Its
#include <iostream> not <iostream.h>.
Didn't you get any error like "Cannot open include file: 'iostream.h': No such file or directory".
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am sorry in advance if I do a terrible job of explaining what my problem is. I am also sorry about the length of this post. This is my first time trying to ask a question on this site and I'm a bit nervous about asking questions in general. I am working on a program to make a stack data structure using a linked list for the college Data Structures class I am taking and have encountered an error in my program which is causing my entire program not to work. We used a couple of files from a program we did recently to create the project. If it helps anything I am using Visual Studio 2019. Any help would be appreciated and I thank any of you who took the time to read through all of this.
Here is my first header file: "SingleLinkedList Head.h"
#include <string>
typedef string Item; //This is the line which causes the problem. It keeps saying that there is a
//missing type specifier, yet the type is clearly specified as "string"?
class StringNode {
private:
Item item; //The error above is causing some of the things in my program which
StringNode* next; //use "Item" as the datatype to also give me a C4430 Error
friend class StringLinkedList;
};
class StringLinkedList {
public:
StringLinkedList();
~StringLinkedList();
bool vacant() const; //Checks if the list is empty
const Item& topDeck() const; //Displays the front item of the list (C4430 Error)
void drawCard(const Item& item); //Adds an item to the front of the list (C4430 Error)
void discard(); //Removes an item from the front of the list
void showHand(); //Prints the entire list
private:
StringNode* head;
};
Here is my first implementation file: "SingleLinkedList Imp.cpp"
#include "SingleLinkedList Head.h"
#include <iostream>
using namespace std;
StringLinkedList::StringLinkedList()
:head(NULL) {}
StringLinkedList::~StringLinkedList() {
while (!vacant()) discard();
}
void StringLinkedList::discard() {
StringNode* excess = head;
head = head->next;
delete excess;
}
bool StringLinkedList::vacant() const { return head == NULL; }
const Item& StringLinkedList::topDeck() const { return head->item; } //C4430 Error
void StringLinkedList::drawCard(const Item& i) { //C4430 Error
StringNode* draw = new StringNode;
draw->item = i;
draw->next = head;
head = draw;
}
void StringLinkedList::showHand() {
StringNode* card = head;
while (card != nullptr) {
cout << card->item;
card = card->next;
}
cout << endl << endl;
}
Here is my second header file: "LinkedStack Head.h"
#include "SingleLinkedList Head.h"
class LinkedStack
{
public:
LinkedStack();
int size() const;
bool empty() const;
const Item& top(); //Displays the top of the stack
void stack(const Item& item); //Adds an item to the top of the stack
void unStack(); //Removes an item from the top of the stack
private:
StringLinkedList S;
int n;
};
Here is my second implementation file: "LinkedStack Imp.cpp"
#include "LinkedStack Head.h"
#include <iostream>
using namespace std;
LinkedStack::LinkedStack()
: S(), n(0) {}
int LinkedStack::size() const { return n; }
bool LinkedStack::empty() const { return n == 0; }
const Item& LinkedStack::top() { return S.topDeck(); }
void LinkedStack::stack(const Item& i) {
++n;
S.drawCard(i);
}
void LinkedStack::unStack() {
if (empty())
cout << "The stack is empty.";
else
{
--n;
S.discard();
}
}
Here is my main file: "LinkedStack Main.cpp"
#include "LinkedStack Head.h"
#include <iostream>
using namespace std;
int main()
{
LinkedStack B;
B.stack("Bob");
if (!B.empty())
cout << B.top() << endl;
else
cout << "The stack is empty.";
B.stack("Devin");
B.stack("Claire");
B.stack("Malachi");
B.stack("Diana");
B.unStack();
B.unStack();
B.unStack();
return 0;
}
I don't exactly know what is causing this to happen. I am also getting some strange errors telling me to put semicolons in strange places (one asked my to put a semicolon before "item" in the typedef line, which doesn't make much sense). My best guess is that somewhere in my program I have overlooked a mistake of mine and the program just needs to be looked over with a fresh pair of eyes.
Note: I am aware that using "using namespace std;" is kind of a cardinal sin when it comes to programming. I used it in my code because it was used in the examples we did in class. I will shift away from using it in programs which aren't based on such examples (i.e. programs that are entirely my creation).
You are missing the std namespace in "SingleLinkedList Head.h". Use typedef std::string Item;. You keep getting "missing type specifier" because "string" isn't defined yet(so it is not a type spcifier), the compiler "thinks" that you are writing typedef <sometype> string but <sometype> is missed.
Note: using namespace std; is another possible legal fix that will be accepted by compilers but it is strongly not recommended in header files.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The following Code should create a graph. For that, there are two classes which are edge and graph.
Our Problem is, that the program compiles, but crashes..
In Visual Studio gives the error, that there is a read access violation exception.
We guess, that this comes from the pointer e, beacause this is used wrong. But we donĀ“t know what is actually wrong.
Question: Are the pointers the reason why the program crashes and for the read access violation exception? And what shold be corrected?
We'll appreciate any help and code corrections.
CODE:
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
class edge {
public:
int s,t;
edge(int s, int t) : s(s), t(t) {}
};
class graph {
public:
int m,n;
vector<edge*> *e;
graph(int n, int m) : n(n), m(m) {
for (int i=0; i<m; i++) {
(*e).push_back(new edge(i,-1));
}
}
~graph() {
for(int i=m-1;i>=0; i--) {
delete [] (*e)[i];
}
}
void make_edge(int i, int s, int t) {
for(i=0;i<m;i++) {
assert(((*e)[i]->s!=s)&&((*e)[i]->t!=t));
}
assert(s!=t);
assert(s+1==t);
}
};
int main() {
const int n=4;
const int m=3;
graph g(n,m);
g.make_edge(0,0,1);
g.make_edge(1,1,2);
g.make_edge(2,2,3);
return 0;
}
Your class graph is using member e, which is a pointer to class of type std::vector<edge*>', however this member e was not allocated prior to usage in constructor. This is why the crash
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
given the next code:
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
class A {
public:
virtual ~A() {
}
};
class B: public A {
public:
};
int main() {
int n = 4;
A a;
A& base = a;
B* ptr = dynamic_cast<B*>(&base);
if (ptr == NULL) {
cerr << "base is not a B";
}
try {
B& derived = dynamic_cast<B&>(base);
derived = *ptr;
} catch (std::bad_cast&) { // ERROR
cerr << "base is not a B";
}
if (n == 3) {
}
return 0;
}
I get this message error and I don't understand what is the reason and how can I fix it?
'bad_cast' in namespace 'std' does not name a type
If you look up the documentation at http://en.cppreference.com/w/cpp/types/bad_cast it tells you at the top which include is required for each class/function. In this case you need to include <typeinfo>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Here is the a.h header:
#include <string>
template <typename L>
class A
{
L l;
public:
A() : l("a-text") {}
const std::string get() const { l.get(); } // <<<< Edit: missing return!
};
And here is a.cpp:
#include "a.h"
#include <iostream>
class L {
const std::string v;
public:
L(const std::string& v_): v(v_) {}
const std::string get() const { return v; }
};
int main() {
L l("l-text");
std::cout << l.get().c_str() << std::endl;
A<L> a;
std::cout << a.get().c_str() << std::endl; // <<<< - this will report Segmentation fault
return 0;
}
The first std::cout will work okay displaying l-text
Yet the second std::cout will report Segmentation fault instead of displaying a-text.
Thanks in advance!
Turn on your warnings. You're missing a return statement here:
const std::string get() const { l.get(); }
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm not sure why I am getting an error during compilation that says "error: "size" declared as function returning a function" when size() is returning a type size_t. Any help would be appreciated, thanks.
// Text.h
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
namespace w3 {
class Text {
string* arrayRecords;
size_t numRecords;
public:
Text();
Text(const char* fileName);
size_t size() const;
~Text();
};
}
// Text.cpp
#include "Text.h"
namespace w3 {
Text::Text() {
numRecords = 0;
arrayRecords = nullptr;
}
Text::Text(const char* fileName) {
//
}
size_t Text::size() const() {
return numRecords;
}
Text::~Text() {
if(arrayRecords)
delete [] arrayRecords;
}
}
The problem is with this line:
size_t Text::size() const()
Remove the () after const, so you have:
size_t Text::size() const