lundi 8 mai 2017

C++: Undefined Symbol when method declared in cpp body

I have a simple C++ class with a method that I want to Call from a Google Test Fixture.

When I declare the method in the cpp file the compiler throws an undefined symbol error:

Class.h

class Class {
 public:
  double test() {
    return 1.;
  }
}

Class.cpp

double Class::test() {
    return 1.;
}

GoogleTest.cpp

class GoogleTest : public ::testing:Test {
 protected:
  Class c;
}

TEST_F(GoogleTest, TestIt) {
  EXPECT_EQ(c.test(), 1.);
}


Meanwhile defining the method in the header I can compile like a charm:

Class.h

class Class {
 public:
  double test() {
    return 1.;
  }
}

GoogleTest.cpp

class GoogleTest : public ::testing:Test {
 protected:
  Class c;
}

TEST_F(GoogleTest, TestIt) {
  EXPECT_EQ(c.test(), 1.);
}

What is the cause of this behavior? I do not want to define all my methods in header.

Aucun commentaire:

Enregistrer un commentaire