Doing simple template code given ; just that I am writing separate declaration and definition  This is my header file  template &lt;typename T&gt; class SmartPointerGen { private:         T*  pData;  public:         SmartPointerGen(T* pValue);         ~SmartPointerGen();          T&amp;  operator* ();         T*  operator-&gt; (); };   This is how I define the methods      #include "SmartPointer_Generic.h"      template &lt;typename T&gt;      SmartPointerGen&lt;T&gt;::SmartPointerGen(T* pValue) :    pData(pValue)     {      }      template &lt;typename T&gt;     SmartPointerGen&lt;T&gt;::~SmartPointerGen()     {         delete pData;     }      template &lt;typename T&gt;     T&amp;  SmartPointerGen&lt;T&gt;::operator* ()     {         return *pData;     }      template &lt;typename T&gt;     T*  SmartPointerGen&lt;T&gt;::operator-&gt; ()     {         return pData;     }   And in main(), I try to use the same with  SmartPointerGen&lt;Person&gt; pPersonGen(new Person("WTF Again"));  I believe the problem is with the definition of the SmartPointerGen class, I tried following  syntax for defining templatized methods outside class, but the linker is saying unresolved symbols for destructor and overloaded operators, (BUT NOT FOR THE CONSTRUCTOR).  errors:(showing only 1, others are quite similar)  main.obj : error LNK2019: unresolved external symbol "public: __thiscall SmartPointerGen&lt;class Person&gt;::SmartPointerGen&lt;class Person&gt;(class Person *)" (??0?$SmartPointerGen@VPerson@@@@QAE@PAVPerson@@@Z) referenced in function _main
341
Quote from :     The only portable way of using templates at the moment is to implement them in header files by using inline functions.   Why is this?  (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)
20526
It is to my understanding that one should use a forward-class declaration in the event ClassA needs to include a ClassB header, and ClassB needs to include a ClassA header to avoid any circular inclusions. I also understand that an #import is a simple ifndef so that an include only happens once.  My inquiry is this: When does one use #import and when does one use @class? Sometimes if I use a @class declaration, I see a common compiler warning such as the following:     warning: receiver 'FooController' is a forward class and corresponding @interface may not exist.   Would really love to understand this, versus just removing the @class forward-declaration and throwing an #import in to silence the warnings the compiler is giving me.
13425
I'm trying to write a blog post which includes a code segment inside a &lt;pre&gt; tag. The code segment includes a generic type and uses &lt;&gt; to define that type. This is what the segment looks like:  &lt;pre&gt;     PrimeCalc calc = new PrimeCalc();     Func&lt;int, int&gt; del = calc.GetNextPrime; &lt;/pre&gt;   The resulting HTML removes the &lt;&gt; and ends up like this:  PrimeCalc calc = new PrimeCalc(); Func del = calc.GetNextPrime;   How do I escape the &lt;&gt; so they show up in the HTML?
69483
I have code like this:  template &lt;typename T, typename U&gt; struct MyStruct {     T aType;     U anotherType; };  class IWantToBeFriendsWithMyStruct {     friend struct MyStruct; //what is the correct syntax here ? };   What is the correct syntax to give friendship to the template ?
24839
class Outer {      class Inner {     public:         Inner() {}         void func() ;     };  private:     static const char* const MYCONST;     int var; };  void Outer::Inner::func() {     var = 1; }  const char* const Outer::MYCONST = "myconst";   This errors out when I compile with class Outer::Inner' has no member named `var'
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}
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; }
6108
Why do I get compiler errors with this Java code?  1  public List&lt;? extends Foo&gt; getFoos() 2  { 3    List&lt;? extends Foo&gt; foos = new ArrayList&lt;? extends Foo&gt;(); 4    foos.add(new SubFoo()); 5    return foos; 6  }  Where 'SubFoo' is a concrete class that implements Foo, and Foo is an interface.  Errors I get with this code:   On Line 3: "Cannot instantiate ArrayList&lt;? extends Foo&gt;" On Line 4: "The method add(capture#1-of ? extends Foo) in the type List&lt;capture#1-of ? extends Foo&gt; is not applicable for the arguments (SubFoo)"   Update: Thanks to Jeff C, I can change Line 3 to say "new ArrayList&lt;Foo&gt;();".  But I'm still having the issue with Line 4.
16896
I have a generic class that I'm trying to implement implicit type casting for.  While it mostly works, it won't work for interface casting.  Upon further investigation, I found that there is a compiler error: "User-defined conversion from interface" that applies.  While I understand that this should be enforced in some cases, what I'm trying to do does seem like a legitimate case.  Here's an example:  public class Foo&lt;T&gt; where T : IBar {     private readonly T instance;      public Foo(T instance)     {         this.instance = instance;     }     public T Instance     {         get { return instance; }     }     public static implicit operator Foo&lt;T&gt;(T instance)     {         return new Foo&lt;T&gt;(instance);     } }   Code to use it:  var concreteReferenceToBar = new ConcreteBar(); IBar intefaceReferenceToBar = concreteReferenceToBar; Foo&lt;ConcreteBar&gt; concreteFooFromConcreteBar = concreteReferenceToBar; Foo&lt;IBar&gt; fooFromConcreteBar = concreteReferenceToBar; Foo&lt;IBar&gt; fooFromInterfaceBar = intefaceReferenceToBar; // doesn't work   Does anyone know a workaround, or can anyone explain in a satisfactory way why I shuouldn't be able to cast interfaceReferenceToBar implicitly to Foo&lt;IBar&gt;, since in my case it is not being converted, but only contained within Foo?  EDIT: It looks like covariance might offer salvation.  Let's hope the C# 4.0 specification allows for implicit casting of interface types using covariance.
35935
I'm using the 'using' declaration in C++ to add std::string and std::vector to the local namespace (to save typing unnecessary 'std::'s).  using std::string; using std::vector;  class Foo { /*...*/ };   What is the scope on this declaration?  If I do this in a header, will it inject these 'using' declarations into every cpp file that includes the header?
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.
14535
I am looking for the definition of when I am allowed to do forward declaration of a class in another class's header file:  Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc. ?
99300
So, no matter what I seem to do, I cannot seem to avoid having Dev C++ spew out numerous Multiple Definition errors as a result of me including the same header file in multiple source code files in the same project.  I'd strongly prefer to avoid having to dump all my source code into one file and only include the header once, as that's going to make my file very long and difficult to manage.  Essentially, this is what's going on:  #ifndef _myheader_h #define _myheader_h  typedef struct MYSTRUCT{ int blah; int blah2; } MYSTRUCT;  MYSTRUCT Job_Grunt; MYSTRUCT *Grunt = &amp;Job_Grunt; MYSTRUCT Job_Uruk; MYSTRUCT *Uruk = &amp;Job_Grunt;  int Other_data[100];  void load_jobs();  #endif   Example Cpp File (They pretty much all look something like this):  #include "myheader.h"  void load_jobs(){  Grunt-&gt;blah = 1; Grunt-&gt;blah2 = 14;  Uruk-&gt;blah = 2; Uruk-&gt;blah2 = 15;  return; }   Bear in mind that I have about 5 cpp files that include this one header, each one dealing with a different type of struct found in the header file.  In this example there was only the one struct containing a couple of members, when there are about 4-6 different structs with many more members in the actual header file.  All the files I've included it in follow the same formula as you see in this example here.  Now I understand that the header guard only stops each individual cpp file from including the header file more than once.  What would seem to be happening is that when the compiler reads the include at the start of each cpp, it defines the header file all over again, which is causing it to spit out lines and lines of:  Multiple Definition of Uruk, first defined here   Multiple Definition of Job_Uruk, first defined here   Multiple Definition of Grunt, first defined here   Multiple Definition of Job_Grunt, first defined here   Multiple Definition of Other_data, first defined here   I'll see a set of this for just about every cpp file in the project which includes the header.  I've tried moving the definitions of the struct and the struct variables to the cpp files, but then the other cpp files cannot see them or work with them, which is very important as I need all files in the project to be able to work with these structs.  But the single most confusing part about this problem requires a little more explanation:  The way I'm setting up these multiple files in this project is identical to the book I'm working with, All In One Game Programming by John S. Harbour.  I ran into the exact same problems when I created the files for example projects in the book which called for one header included by multiple cpps in the same project.  I could type them out, word for word from the book, and I do mean word for word... and I'd get the series of MD errors for every cpp in the project.    If I loaded the example project from the CD included with the book, it would compile and run without a problem, allthough the files themselves, as well as the project options, were by all appearances identical to the ones I had created.  If I created my own project file, and simply added the source and header files for the example project from the CD, this, too, would also compile and run, though I can find no difference between those and mine.  So then, I tried making my own project file, then creating the blank source and header files and adding them to it, and then filling them by copying and pasting their contents from the files on the CD they were meant to correspond to(the same ones that had worked). And sure enough, I'd get the same thing...lines and lines of MD error messages.  I'm absolutely baffled.  I've repeated all these methods multiple times, and am certain I'm not mistyping or miscopying the code.  There just seems to be something about the premade files themselves; some configuration setting or something else I'm missing entirely...that will cause them to compile correctly while the files I make myself won't.
113468
I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred.  Here is my problem. I have a C++ class -- let's call it "A" -- that currently uses fprintf() to print its heavily formatted data structures to a file pointer. In its print function, it also recursively calls the identically defined print functions of several member classes ("B" is an example). There is another class C that has a member std::string "foo" that needs to be set to the print() results of an instance of A. Think of it as a to_str() member function for A.  In pseudocode:  class A { public:   ...    void print(FILE* f);   B b;    ...   };  ...  void A::print(FILE *f) {   std::string s = "stuff";   fprintf(f, "some %s", s);   b.print(f); }  class C {   ...   std::string foo;   bool set_foo(std::str);   ... }  ...  A a = new A(); C c = new C();  ...  // wish i knew how to write A's to_str() c.set_foo(a.to_str());   I should mention that C is fairly stable, but A and B (and the rest of A's dependents) are in a state of flux, so the less code changes necessary the better. The current print(FILE* F) interface also needs to be preserved. I have considered several approaches to implementing A::to_str(), each with advantages and disadvantages:   Change the calls to fprintf() to sprintf()   I wouldn't have to rewrite any format strings print() could be reimplemented as: fprint(f, this.to_str()); But I would need to manually allocate char[]s, merge a lot of c strings , and finally convert the character array to a std::string  Try to catch the results of a.print() in a string stream   I would have to convert all of the format strings to &lt;&lt; output format. There are hundreds of fprintf()s to convert :-{ print() would have to be rewritten because there is no standard way that I know of to create an output stream from a UNIX file handle (though ).  Use Boost's string    More external dependencies. Yuck. Format's syntax is different enough from printf() to be annoying:    printf(format_str, args) -> cout &lt;&lt; boost::format(format_str) % arg1 % arg2 % etc Use Qt's    A different external dependency.    So, have I exhausted all possible options? If so, which do you think is my best bet? If not, what have I overlooked?  Thanks.
81239
I'm a C++ developer - not a java developer, but have to get this code working...  I have 2 public classes that will be used by another product.  I used the package directive in each of the java files.    package com.company.thing;  class MyClass ...   When I try to compile a test app that uses that I add   import com.company.thing.*;   The javac compiler fails with errors about com.company does not exist.  (even if I compile it in the same directory as the class files I just made a package of)  I am sure I am doing something bone-headed and silly.  I've read the  pages and tried to set up a directory structure like /com/company/thing  etc, but either I have totally screwed it all up or am missing something else.  EDIT thanks for the suggestions - I had tried the classpath previously.  It does not help.  I tried compiling   javac -classpath &lt;parent&gt; client.java    and the result is:  package com.company does not exist   I have the code I want to import (the two java files) in \com\company\product.  I compile those fine.  (they contain MyClass) I even made a jar file for them.  I copied the jar file up to the parent directory.  I then did (in the parent directory with the client java file)   javac -cp &lt;jarfile&gt; *.java   the result is:  cannot access MyClass bad class file: MyClass.class(:MyClass.class) class file contains wrong class: com.company.product.MyClass Please remove or make sure it appears in the correct subdirectory of the classpath.   EDIT  I got the client code to compile and run if I used the fully qualified name for MyClass and compiled it in the parent directory.  I am totally confused now.    compiled with no classpath set - just   javac *.java    in the parent directory - and it worked fine.  I can get a test app to compile, but that is not going to cut it when i have to integrate it into the production code.  Still looking for help.  EDIT:  Finally - not sure why it didn't work before - but I cleaned up all the files all over the directory structure and now it works.    Thanks
105374
Backgrounder:  The  (Pointer to IMPLementation) is a technique for implementation hiding in which a public class wraps a structure or class that cannot be seen outside the library the public class is part of.  This hides internal implementation details and data from the user of the library.  When implementing this idiom why would you place the public methods on the pimpl class and not the public class since the public classes method implementations would be compiled into the library and the user only has the header file?  To illustrate, this code puts the Purr() implementation on the impl class and wraps it as well.  Why not implement Purr directly on the public class?  // header file: class Cat {     private:         class CatImpl;  // Not defined here         CatImpl *cat_;  // Handle      public:         Cat();            // Constructor         ~Cat();           // Destructor         // Other operations...         Purr(); };   // CPP file: #include "cat.h"  class Cat::CatImpl {     Purr(); ...     // The actual implementation can be anything };  Cat::Cat() {     cat_ = new CatImpl; }  Cat::~Cat() {     delete cat_; }  Cat::Purr(){ cat_-&gt;Purr(); } CatImpl::Purr(){    printf("purrrrrr"); }
36729
I am trying to pass a member function within a class to a function that takes a member function class pointer.  The problem I am having is that I am not sure how to properly do this within the class using the this pointer.  Does anyone have suggestions?  Here is a copy of the class that is passing the member function:  class testMenu : public MenuScreen{ public:  bool draw;  MenuButton&lt;testMenu&gt; x;  testMenu():MenuScreen("testMenu"){     x.SetButton(100,100,TEXT("buttonNormal.png"),TEXT("buttonHover.png"),TEXT("buttonPressed.png"),100,40,&amp;this-&gt;test2);      draw = false; }  void test2(){     draw = true; } };   The function x.SetButton(...) is contained in another class, where "object" is a template.  void SetButton(int xPos, int yPos, LPCWSTR normalFilePath, LPCWSTR hoverFilePath, LPCWSTR pressedFilePath, int Width, int Height, void (object::*ButtonFunc)()) {      BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);      this-&gt;ButtonFunc = &amp;ButtonFunc; }   If anyone has any advice on how I can properly send this function so that I can use it later.