0

I am trying to get a constructor to accept string, and failing.

To simplify it, I brought my code to a bare minimum.. My code is as follows:

file: TestClass.h

#ifndef __TestClass_H__
#define __TestClass_H__

#include <string.h>
using namespace std;

class TestClass
{
    public:
        TestClass(string TSTStr);
};

#endif /* __TestClass_H__ */

file: TestClass.cpp

#include "TestClass.h"

TestClass::TestClass(string TSTStr) 
{
}

And my main code simply has...

#include "TestClass.h"
#include <string>
using namespace std;

string TestString = "test";
TestClass TC1(TestString);

Yet, when I try to compile, I get the following error:

In file included from src\main.cpp:28:
lib/TestClass/src/TestClass.h:10:19: error: expected ')' before 'TSTStr'
   TestClass(string TSTStr);
            ~      ^~~~~~~
                   )
src\main.cpp: In function 'void app_main_JSON()':
src\main.cpp:113:26: error: no matching function for call to 'TestClass::TestClass(std::__cxx11::string&)'
  TestClass TC1(TestString);
                          ^
In file included from src\main.cpp:28:
lib/TestClass/src/TestClass.h:7:7: note: candidate: 'constexpr TestClass::TestClass()'
 class TestClass
       ^~~~~~~~~
lib/TestClass/src/TestClass.h:7:7: note:   candidate expects 0 arguments, 1 provided
lib/TestClass/src/TestClass.h:7:7: note: candidate: 'constexpr TestClass::TestClass(const TestClass&)'
lib/TestClass/src/TestClass.h:7:7: note:   no known conversion for argument 1 from 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'const TestClass&'
lib/TestClass/src/TestClass.h:7:7: note: candidate: 'constexpr TestClass::TestClass(TestClass&&)'
lib/TestClass/src/TestClass.h:7:7: note:   no known conversion for argument 1 from 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'TestClass&&'

I am following a sample code I've found here, which works : https://www.w3schools.com/cpp/cpp_constructors.asp

I'm not sure what I'm doing wrong :/

Note: I am using platformio over VSCode.

0 Answers0