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
#include <iostream>
using namespace std;
class B;
class A
{
int a = 10;
int b = 20;
public:
friend void A::display(B&a);
void display(B & c);
};
class B
{
int abc = 20;
public:
friend void A::display(B&a);
};
void A::display(B & c)
{
cout << c.abc;
}
int main()
{
B b;
A a;
a.display(b);
return 0;
}
it is giving the error on the 21th line that "abc" is inaccessible. Although it is a friend function and have access to B class' private data members
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 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
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
#include < string.h >
class LongLongTripDiv2
{
public:
std::string isAble(long D, int T, int B)
{
//....
//...
}
};
The above code gives an error: ‘String’ in namespace ‘std’ does not name a type std::String isAble(long D, int T, int B)
I have tried everything "using namespace std" and "using std::string but it still doesn't works.
Your include statement is wrong.
#include <string>
class LongLongTripDiv2
{
public:
std::string isAble(long D, int T, int B);
};