I am new in C++ and I have a basic question for you. It must be basic, I think… 🙂 .
Here is the code that I expect to work but it does not.
char* deneme = Tool::getStringIntoArray("Hello");
I want to get an char array from a static function in the class ‘Tool’ by passing a string into it.
Here is my Tool header:
#include <string>
using namespace std;
#ifndef TOOL_H_
#define TOOL_H_
class Tool
{
public:
static char* getStringIntoArray(string);
};
#endif
I have learnt that the only thing to use that function is to give a keyword which is ‘static’ there.
Finally, the Tool.cpp for the function:
#include "Tool.h"
char* getStringIntoArray(string str)
{
int* size = new int(str.size());
char* array;
array = new char[*size];
for ( int i = 0; i < *size; i++) {
array[i] = str[i];
}
delete size;
return array;
}
Nevertheless it works in the source file where the first code that I gave but when I put this function into a class for static usage, it does not work. The error is shown below :
undefined reference to `Tool::getStringIntoArray(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Thx.
It’s looking for
Tool::getStringIntoArraybut you definedgetStringIntoArray.Change the signature in the definition from
to