Click here to Skip to main content
16,004,927 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionWindows Media Player Pin
Harshabhi17-Apr-06 15:23
Harshabhi17-Apr-06 15:23 
AnswerRe: Windows Media Player Pin
Stephen Hewitt17-Apr-06 16:23
Stephen Hewitt17-Apr-06 16:23 
QuestionRe: Windows Media Player Pin
Harshabhi18-Apr-06 14:19
Harshabhi18-Apr-06 14:19 
AnswerRe: Windows Media Player Pin
Stephen Hewitt18-Apr-06 14:22
Stephen Hewitt18-Apr-06 14:22 
QuestionRe: Windows Media Player Pin
Harshabhi19-Apr-06 6:38
Harshabhi19-Apr-06 6:38 
AnswerRe: Windows Media Player Pin
Stephen Hewitt19-Apr-06 13:46
Stephen Hewitt19-Apr-06 13:46 
QuestionRe: Windows Media Player Pin
Harshabhi20-Apr-06 7:09
Harshabhi20-Apr-06 7:09 
Questionproblem exporting static template data members, but not methods Pin
Marcello17-Apr-06 14:40
Marcello17-Apr-06 14:40 
Hello,

Visual Studio 2005

From the help on: Compiler Warning (level 2) C4356
If I modify that example like:
#include <iostream>

template <class T>
class C {
   static int n;
};

class D : C<int> {};

// int D::n = 0; // C4356
int C<int>::n = 0;


if I take this code and put it in a DLL
like this:

// file mydll.h :
    #    if defined(MYLIB_EXPORTS)
    #        define MYLIB_API __declspec(dllexport)
    #    else
    #        define MYLIB_API __declspec(dllimport)
    #    endif

template <class T>
class C {
   static int n;
   static void show() {
      std::cout << "C:show()" << std::endl;
      std::cout << n << std::endl;
   }
};

class MYLIB_API D : C<int> {};


// file mydll.cpp :

// initialization of static member
int C<int>::n = 3;


// file myapp.cpp :

#include "mydll.h"

int main() {

   int k1 = D::n;      // (a) error LNK2001: unresolved external symbol
   int k2 = C<int>::n; // (b) error LNK2001: unresolved external symbol

   D::show(); // Okay
}


The error I get, if I uncomment (a) or (b), is always:
1>myapp.obj : error LNK2001: unresolved external symbol "public: static int C<int>::n" (?n@?$C@H@@2HA)

This makes sense because I am not exporting C.

So the question is:
1.1) Why then method show() gets exported ?
1.2) In other words: Why the method gets exported and the data member not ?

2) Is there any way to export directly the data ?
( of course I could workaround the problem and access the static data member through a static method )

3) A last question, a little different, but maybe related:
If I try to export some data from a DLL, like:

// file mydll.h :
static MYLIB_API int mydata; // error C2201: 'mydata' : must have external linkage in order to be exported/imported
MYLIB_API static int mydata2; // error C2201: 'mydata' : must have external linkage in order to be exported/imported

I think that with vc70, if I remember well, I was not gettting any error message while compiling the DLL, but the LNK2001 error when linking the application.

In other words I am always forced to export data as static data member of a class, ( which is in some cases very unconvenient), like:
// file mydll.h :
class MYLIB_API MyDataClass {
  static int data;
};

// file mydll.cpp :
int MyDataClass::data = 5;


Is there a way to do it without using a class ?


Thank you very much for any answer.
Marcello
AnswerRe: problem exporting static template data members, but not methods Pin
Maxwell Chen17-Apr-06 17:14
Maxwell Chen17-Apr-06 17:14 
GeneralRe: problem exporting static template data members, but not methods Pin
Stephen Hewitt17-Apr-06 19:56
Stephen Hewitt17-Apr-06 19:56 
GeneralRe: problem exporting static template data members, but not methods Pin
Maxwell Chen17-Apr-06 20:08
Maxwell Chen17-Apr-06 20:08 
AnswerRe: problem exporting static template data members, but not methods Pin
Branislav17-Apr-06 23:32
Branislav17-Apr-06 23:32 
AnswerRe: problem exporting static template data members, but not methods Pin
Marcello18-Apr-06 9:53
Marcello18-Apr-06 9:53 
GeneralRe: problem exporting static template data members, but not methods Pin
Branislav18-Apr-06 18:53
Branislav18-Apr-06 18:53 
GeneralRe: problem exporting static template data members, but not methods Pin
Marcello21-Apr-06 11:32
Marcello21-Apr-06 11:32 
QuestionLow level operations to devices Pin
fvalerin17-Apr-06 14:02
fvalerin17-Apr-06 14:02 
AnswerRe: Low level operations to devices Pin
David Crow18-Apr-06 3:06
David Crow18-Apr-06 3:06 
GeneralRe: Low level operations to devices Pin
fvalerin18-Apr-06 4:44
fvalerin18-Apr-06 4:44 
GeneralRe: Low level operations to devices Pin
David Crow18-Apr-06 4:52
David Crow18-Apr-06 4:52 
GeneralRe: Low level operations to devices Pin
Marcello18-Apr-06 10:39
Marcello18-Apr-06 10:39 
GeneralRe: Low level operations to devices Pin
David Crow18-Apr-06 10:44
David Crow18-Apr-06 10:44 
GeneralRe: Low level operations to devices Pin
Marcello18-Apr-06 11:15
Marcello18-Apr-06 11:15 
Questionhow to change the color of title bar and its text , in dialog application Pin
Hannan Azam17-Apr-06 13:49
Hannan Azam17-Apr-06 13:49 
AnswerRe: how to change the color of title bar and its text , in dialog application Pin
includeh1017-Apr-06 16:21
includeh1017-Apr-06 16:21 
AnswerRe: how to change the color of title bar and its text , in dialog application Pin
Hamid_RT17-Apr-06 19:12
Hamid_RT17-Apr-06 19:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.