Hey guys here is some code I am going to run, issue is it doesn't work the way I intend to. I am unable to figure out what's wrong with it. I am c++ noob please help.  #include &lt;iostream&gt; #include &lt;cmath&gt; #include &lt;stdexcept&gt; using namespace std;  /**  * Super class  */ class Shape { protected:     int _dimensions; public:     Shape() : _dimensions{0}     {};     Shape(int dims) : _dimensions{dims}     {};      virtual double getArea() {};     // Because some shapes have no volume.       virtual double getVolume() {};        void setDimensions(int dim)     {         this-&gt;_dimensions = dim;     };     int getDimensions()     {         return this-&gt;_dimensions;     }; };   /**   * Extended classes  */ class TwoDimensionalShape : public Shape { public:      TwoDimensionalShape() : Shape{2}     {};     // This should throw an error     double getVolume() {         throw logic_error("This shape ain't got area!");     };   };  class ThreeDimensionalShape : public Shape { public:      ThreeDimensionalShape() : Shape{3} {}; };   /**  * Final Concrete classes extending extended classes  */ class Circle : public TwoDimensionalShape { protected:     double _radius; public:     Circle(double r) : _radius{r}     {};     double getArea()     {         // pi*r^2         return M_PI * pow(_radius, 2);     } };  class Square : public TwoDimensionalShape { protected:     double _side; public:     Square(double s) : _side{s}     {}     double getArea()     {         // s^2         return pow(_side, 2);     } };  class Triangle : public TwoDimensionalShape { protected:     double _base, _height; public:     Triangle(double b, double h) : _base{b}, _height{h}     {};     double getArea()     {         // b*h/2         return _base * _height / 2;     } };  class Sphere : public ThreeDimensionalShape { protected:     double _radius; public:     Sphere(double r) : _radius{r}     {}     double getArea()     {         cout &lt;&lt; 4 * M_PI * pow(_radius, 2) &lt;&lt; endl;         return 4 * M_PI * pow(_radius, 2);     }     double getVolume()     {         return (4/3) * M_PI * pow(_radius, 3);     } };  class Cube : public ThreeDimensionalShape { protected:     double _side; public:     Cube(double s) : _side{s}     {};     double getArea()     {         // surface area = 6*a^2         return 6 * pow(_side, 2);     }     double getVolume()     {         // a^3         return pow(_side, 3);     } };  class Tetrahedron : public ThreeDimensionalShape { protected:     double _side; public:     Tetrahedron(double s) : _side{s}     {};     double getArea()     {         // sqrt(3)*a^2         return sqrt(3) * pow(_side, 2);     }     double getVolume()     {         // a^3/6sqrt(2)         return pow(_side, 3) / (6 * sqrt(2));     } };  int main() {     Shape arr[2];     arr[0] = Circle{10};     arr[1] = Sphere{10};      // This one is accessing the right method.     cout &lt;&lt; "Area of circle: " &lt;&lt; arr[0].getArea() &lt;&lt; endl;      // This one should access the parent, but accesses the grand parent!     // even if it is overridden in parent.     cout &lt;&lt; "Volume of circle: " &lt;&lt; arr[0].getVolume() &lt;&lt; endl;       // Both of these are accessing methods on grand parent rather than their own!!     cout &lt;&lt; "Area of sphere: " &lt;&lt; arr[1].getArea() &lt;&lt; endl;     cout &lt;&lt; "Volume of sphere: " &lt;&lt; arr[1].getVolume() &lt;&lt; endl;       return 0; }   I don't know why the array methods keep accessing the grand parent functions in the last three lines, but the right method on the first.
664
Someone mentioned it in the IRC as the slicing problem.
24549
So I'm refactoring my code to implement more OOP.  I set up a class to hold page attributes.  class PageAtrributes  {   private $db_connection;   private $page_title;      public function __construct($db_connection)      {         $this-&gt;db_connection = $db_connection;         $this-&gt;page_title = '';     }      public function get_page_title()     {         return $this-&gt;page_title;     }      public function set_page_title($page_title)     {         $this-&gt;page_title = $page_title;     } }   Later on I call the set_page_title() function like so  function page_properties($objPortal) {         $objPage-&gt;set_page_title($myrow['title']); }   When I do I receive the error message:     Call to a member function set_page_title() on a non-object   So what am I missing?
17714
I have a function with the same name, but with different signature in a base and derived classes. When I am trying to use the base class's function in another class that inherits from the derived, I receive an error. See the following code:  class A {     public:     void foo(string s){}; };  class B : public A {     public:     int foo(int i){}; };  class C : public B {     public:     void bar()     {         string s;         foo(s);     } };   I receive the following error from the gcc compiler:  In member function `void C::bar()': no matching function for call to `C::foo(std::string&amp;)' candidates are: int B::foo(int)   If I remove int foo(int i){}; from class B, or if I rename it from foo1, everything works fine.   What's the problem with this?
42083
What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header files?  I have this situation in my project and solving class definition related errors crossing over several header files has become quite a headache.
1488
Can anyone explain why following code won't compile? At least on g++ 4.2.4.  And more interesting, why it will compile when I cast MEMBER to int?  #include &lt;vector&gt;  class Foo {   public:       static const int MEMBER = 1;   };  int main(){       vector&lt;int&gt; v;       v.push_back( Foo::MEMBER );       // undefined reference to `Foo::MEMBER'     v.push_back( (int) Foo::MEMBER ); // OK       return 0; }
33360
OK, I've been staring at this for an hour.  I know there's an error in this frame, as the document compiles fine if I put \end{document} in the frame before this.  But this frame gives an error   File ended while scanning use of \next   Where is it?????  \begin{frame}[fragile] \frametitle{PROC TEMPLATE for a fancy graph (part 1)} \begin{alltt} proc template;  define statgraph scatdens2;  begingraph;   *BEGIN DEFINING THE GRAPH;    entrytitle "Scatter plot with density plots";     *CREATE A TITLE;    layout lattice/columns = 2 rows = 2     columnweights = (.8 .2) rowweights = (.8 .2)       columndatarange = union rowdatarange = union;    *LAYOUT LATTICE...SETS UP A GRID OF GRAPHS;    *COLUMNWEIGHTS AND ROWWEIGHTS SETS     THE RELATIVE SIZE OF THE INDIVIDUAL COLUMNS AND ROWS;  \end{alltt}  \end{frame}
8090
I have a file: Base.h  class Base; class DerivedA : public Base; class DerivedB : public Base;  /*etc...*/   and another file: BaseFactory.h  #include "Base.h"  class BaseFactory { public:   BaseFactory(const string &amp;sClassName){msClassName = sClassName;};    Base * Create()   {     if(msClassName == "DerivedA")     {       return new DerivedA();     }     else if(msClassName == "DerivedB")     {       return new DerivedB();     }     else if(/*etc...*/)     {       /*etc...*/     }   }; private:   string msClassName; };  /*etc.*/   Is there a way to somehow convert this string to an actual type (class), so that BaseFactory wouldn't have to know all the possible Derived classes, and have if() for each one of them? Can I produce a class from this string?   I think this can be done in C# through Reflection. Is there something similar in C++?
50816
I am trying to learn C++, but I can't even make the first (hello world) example from cplusplus.org work with g++ command or c++. I created a file just like the example and ran g++ myfile, then ./myfile.  I get a permission problem, then I use chmod +x myfile and the permission problem is gone but this problem happens:  gzuspower@gzuspower:~/Desktop$ ./4 ./4: line 1: //myfirst: No such file or directory ./4: line 4: syntax error near unexpected token `(' ./4: line 4: `int main ()'   I called my file 4 because it is my 4th attempt. This is on Xubuntu 16.04.The duplicate question answer did not work i tried .c ,.cpp ,as file extension and tried firstc++ (my first attempt file) instead of 4(my fourth attempt file) and nothing works. i'm going to try different form of hello-world code.
8198
I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++.  Here is the relevant part of my matrix class:  namespace Math {     class Matrix     {     public:          [...]          friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; stream, const Matrix&amp; matrix);     } }   And the "implementation":  using namespace Math;  std::ostream&amp; Matrix::operator &lt;&lt;(std::ostream&amp; stream, const Matrix&amp; matrix) {      [...]  }   This is the error given by the compiler:     matrix.cpp:459: error: 'std::ostream&amp;   Math::Matrix::operator&lt;&lt;(std::ostream&amp;,   const Math::Matrix&amp;)' must take   exactly one argument   I'm a bit confused by this error, but then again my C++ has gotten a bit rusty after doing lots of Java those 6 months. :-)
62585
Given the following example, why do I have to explicitly use the statement b-&gt;A::DoSomething() rather than just b-&gt;DoSomething()?  Shouldn't the compiler's overload resolution figure out which method I'm talking about?  I'm using Microsoft VS 2005. (Note: using virtual doesn't help in this case.)  class A {   public:     int DoSomething() {return 0;}; };  class B : public A {   public:     int DoSomething(int x) {return 1;}; };  int main() {   B* b = new B();   b-&gt;A::DoSomething();    //Why this?   //b-&gt;DoSomething();    //Why not this? (Gives compiler error.)   delete b;   return 0; }
222
In several C++ examples I see a use of the type size_t where I would have used a simple int. What's the difference, and why size_t should be better?
21989
Very simply put:  I have a class that consists mostly of static public members, so I can group similar functions together that still have to be called from other classes/functions.  Anyway, I have defined two static unsigned char variables in my class public scope, when I try to modify these values in the same class' constructor, I am getting an "unresolved external symbol" error at compilation.  class test  { public:     static unsigned char X;     static unsigned char Y;      ...      test(); };  test::test()  {     X = 1;     Y = 2; }   I'm new to C++ so go easy on me. Why can't I do this?
80212
For many questions the answer seems to be found in "the standard". However, where do we find that? Preferably online.  Googling can sometimes feel futile, again especially for the C standards, since they are drowned in the flood of discussions on programming forums.  To get this started, since these are the ones I am searching for right now, where are there good online resources for:   C89 C99 C11 C++98 C++03 C++11 C++14 C++17
4097
Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)?
53471
Now that it's clear , there is an associated concept that I use all the time without knowing what it really means.   I suppose everybody made once a mistake with parenthesis, resulting  in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.  Could you give me some explanations, including examples with the magic method ?
11842
I know that according to C++ standard in case the new fails to allocate memory it is supposed to throw std::bad_alloc exception. But I have heard that some compilers such as VC6 (or CRT implementation?) do not adhere to it. Is this true ? I am asking this because checking for NULL after each and every new statement makes code look very ugly.
113807
Why does the following code not work?  I want to instantiate prefabs in a list and then use  it as a GameObject like that :  public GameObject prefab;  void Start() {   ArrayList players = new ArrayList();    for(int i=0; i&lt;5; i++)   {         players.Add( (GameObject)Instantiate(prefab));   }    players[2].transform.position = new Vector3(1, 1, 1);  }   there is an error message