Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指向类模板的指针_C++ - Fatal编程技术网

C++ 指向类模板的指针

C++ 指向类模板的指针,c++,C++,这是可编译的代码,问题仍然存在 #include <iostream> #include <string> template<typename A,typename B,typename C> class Mesh{ public: Mesh(){} Mesh(std::string file){ A foo; std::cout << file <

这是可编译的代码,问题仍然存在

#include <iostream>   
#include <string>    

template<typename A,typename B,typename C>   
class Mesh{   
    public:   
    Mesh(){}   
    Mesh(std::string file){   
    A foo;   
    std::cout <<  file << endl;   
    }   
};    

template<typename A, typename B,typename C>    
class Eq{    
public:    
  Mesh<A,B,C>* pmesh;      
  Eq() {}
  Eq(Mesh<A,B,C> *pmesh_){
      pmesh = pmesh_;
  }
};

template<typename A, typename B,typename C>
class Pipe{
public:
  Mesh<A, B,C> mesh;
  Eq<A, B, C> eq1;

  Pipe(){}
  Pipe(std::string file){
      mesh = Mesh<A, B, C>(file);
      eq1 = Eq<A,B,C>(&mesh);
      std::cout << "P:"<<&mesh << " ";
  }
};

template<typename A,typename B, typename C>
class Simulator {
public:
    Pipe<A,B,C> pipe;

    Simulator(){}
    Simulator(std::string file){
        pipe = Pipe<A,B,C>(file);
        std::cout << "S:"<<&(pipe.mesh)<<" ";
    }
};

using namespace std;
int main() {
    typedef double A;
    typedef double B;
    typedef int  C;

    Simulator< A, B, C> simu("mesh");
}
#包括
#包括
样板
类网格{
公众:
网格(){}
网格(std::字符串文件){
阿福;

std::cout代码的编译版本不会显示相同的行为:

#include <iostream>
template<typename A> class Mesh{public: A foo; };

template<typename A>
struct Eq{
  Mesh<A>* pmesh;
  Eq(Mesh<A> *pmesh_){
      pmesh = pmesh_;
  }
  Eq() {}
};

template<typename A>
struct Pipe{
  Mesh<A> mesh;
  Eq<A> eq1;

  Pipe() {
      mesh = Mesh<A>();
      eq1 = Eq<A>(&mesh);
      std::cout << "P:"<<&mesh << " ";
  }
};
template<typename A>
struct Sim {
    Sim() {
        Pipe<A> pipe;
        std::cout << "S:"<<&(pipe.mesh)<<" ";
    }
};

int main() {
    Sim<int> bar;
}

问题在于你在模拟器构造器中的副本。这段代码应该让事情变得更加明显

这与模板无关

#include <iostream>

struct Mesh{ Mesh() { std::cout << "M:" << this << " ";} };

struct Pipe{
  Mesh mesh;

  Pipe() { std::cout << "PX:" << &mesh << " "; }
  Pipe(int file){
      std::cout << "P1:"<<&mesh << " ";
      mesh = Mesh();
      std::cout << "P2:"<<&mesh << " ";
  }
};

struct Simulator {
    Pipe pipe;

    Simulator(){
        std::cout << "S1:"<<&(pipe.mesh)<<" ";
        pipe = Pipe(2);
        std::cout << "S2:"<<&(pipe.mesh)<<" ";
    }
};

int main() {
    Simulator simu;
}
#包括

结构网格{Mesh(){STD::COUT请显示你实例化模板的代码。当你在它的时候,格式正确。如果这是你的实际代码,编译它,你就有史以来最奇怪的C++编译器!这不是你正在使用的代码;它不会编译。(例如,您试图将
Mesh
分配给
Eq
构造函数中的
Mesh*
。请发布一个实际的可编译示例来说明您的问题?编译错误太多了。
[@foomanchu]$ ./temp
P:0x7fffffffeaf0 S:0x7fffffffeaf0 [@foomanchu]$ 
#include <iostream>

struct Mesh{ Mesh() { std::cout << "M:" << this << " ";} };

struct Pipe{
  Mesh mesh;

  Pipe() { std::cout << "PX:" << &mesh << " "; }
  Pipe(int file){
      std::cout << "P1:"<<&mesh << " ";
      mesh = Mesh();
      std::cout << "P2:"<<&mesh << " ";
  }
};

struct Simulator {
    Pipe pipe;

    Simulator(){
        std::cout << "S1:"<<&(pipe.mesh)<<" ";
        pipe = Pipe(2);
        std::cout << "S2:"<<&(pipe.mesh)<<" ";
    }
};

int main() {
    Simulator simu;
}
:!./temp2
M:0x7fffffffea97 PX:0x7fffffffea97   <-- the setup of Simulator, before the constructor
S1:0x7fffffffea97
    M:0x7fffffffea96  <-- setup of the Pipe object, before the Pipe constructor
    P1:0x7fffffffea96 M:0x7fffffffea95 P2:0x7fffffffea96
S2:0x7fffffffea97
struct Simulator {
    Pipe pipe;

    Simulator(): pipe(2){
        std::cout << "S1:"<<&(pipe.mesh)<<" ";
    }
};