You can not initialize tst_
where you declare it. This can only be done for static const
primitive types. Instead you will need to have a constructor for class test1
.
EDIT: below, you will see a working example I did in ideone.com. Note a few changes I did. First, it is better to have the constructor of test
take a const
reference to string
to avoid copying. Second, if the program succeeds you should return 0
not 1
(with return 1
you get a runtime error in ideone).
#include <iostream> #include <string> using namespace std; class test { public: test(const std::string& s):str(s){}; private: std::string str; }; class test1 { public: test1() : tst_("Hi") {} test tst_; }; int main() { return 0; }
Related Posts:
- Sleep for milliseconds
- Sleep for milliseconds
- What does the explicit keyword mean?
- Struct Constructor in C++?
- C++ struct constructor
- What does the explicit keyword mean?
- Inheriting constructors
- error: lvalue required as unary & operand
- How to print pthread_t
- error: lvalue required as unary & operand
- Why am I getting “undefined reference to main”
- Inheriting constructors
- how to define -std=c++11 as default in g++
- C++ error: no matching constructor for initialization of
- Using O_RDWR vs O_RDONLY | O_WRONLY
- no default constructor exists for class
- Getting error: ISO C++ forbids declaration of with no type
- QltAW.png
- Error: No instance of constructor matches the argument list
- expected constructor, destructor, or type conversion before ‘(’ token
- C++ template constructor
- Implementing a tree in C++
- G++ undefined reference to class::function
- Creating an instance of class
- In C++ can constructor and destructor be inline functions?
- When is it safe to call this-> in constructor and destructor
- C++ error: undefined reference to ‘clock_gettime’ and ‘clock_settime’
- C++ : Exception occurred in script: basic_string::_S_construct NULL not valid
- C++ Cannot call constructor directly in small example
- Here is some error with my .h file which show [Error] unterminated #ifndef when I include my class template in it
- g++ ld: symbol(s) not found for architecture x86_64
- Making my own toString() method on c++ struct
- What is meant with “const” at end of function declaration? [duplicate]
- What’s the C++ version of Java’s ArrayList
- What is the difference between “long”, “long long”, “long int”, and “long long int” in C++?
- What is use of c_str function In c++
- Is there a decent wait function in C++?
- What does T&& (double ampersand) mean in C++11?
- C++ – No matching member function for call to ‘push_back’
- C/C++ JSON parser
- C++ Parsing input string to variables
- ‘cout’ was not declared in this scope
- Segmentation fault error 11 C++
- Why doesn’t C++ have a garbage collector?
- How does ifstream’s eof() work?
- Reverse Contents in Array
- Why would we call cin.clear() and cin.ignore() after reading input?
- Should I learn C before learning C++?
- Comparison with string literal results in unspecified behaviour?
- c++: No instance of overloaded function
- C++ error: Undefined symbols for architecture x86_64
- Visual Studio debugger error: Unable to start program Specified file cannot be found
- Printing the correct number of decimal points with cout
- libstdc++-6.dll not found
- vector
::size_type in C++ - What does ‘\0’ mean?
- cin >> “no operator matches these operands”
- C++ on mac : linker command failed with exit code 1 (use -v to see invocation)
- Correct way to use cin.fail()
- Insert object at index of vector c++
- Read whole ASCII file into C++ std::string
- Reading getline from cin into a stringstream (C++)
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- C++ int float casting
- Arduino sprintf float not formatting
- How to end C++ code
- What is the use of intptr_t?
- Compiling C++ on remote Linux machine – “clock skew detected” warning
- std::wstring VS std::string
- C++ string to enum
- c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)
- why is string not declared in scope
- Warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11?
- Reference to non-static member function must be called
- Call to non-static member function without an object argument compiler error
- How can I assign an array from an initializer list?
- I get this error: “glibc detected”
- How can I get and use the header file
in my C++ program? - cin.eof() functionality
- Drawing Circle with OpenGL
- Run-Time Check Failure #2 – Stack around the variable ‘foo’ was corrupted
- How to implement “Press Any Key To Exit”
- Reading and writing binary file
- Converting string to ASCII
- got “cannot appear in a constant-expression” when using template
- C++ ostream and ofstream conversions
- C++ discards qualifiers
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- Xcode 11.1: iostream’ file not found
- non-member function cannot have cv-qualifier
- too many initializers for ‘int [0]’ c++
- Friend methods error
- Does WPF Work with C++?
- Multiple inputs on one line
- Why do I get: -9223372036854775808
- Return a 2d array from a function
- Linker error: “linker input file unused because linking not done”, undefined reference to a function in that file
- What is the difference among ios::app, out, and trunc in c++?
- Where can I find ‘winmm.lib’ (I’m using Visual Studio 2012)
- Getting the error floating point exception: 8