I am practicing C++ with header files but my .json configurations cannot build my main file into .exe, I've searched trough the VS Code documentation about the C++ .json settings but still couldn't manage to fix the problem...
Please, tell me what am I doing wrong.
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MinGW\\bin\\g++.exe",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x86"
}
],
"version": 4
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
// "-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
-----------------------------------------------------------------------------------------------------------------------------:
Here's my code:
main.cpp:
#include <iostream>
#include "Mystring.h"
using namespace std;
int main() {
cout << boolalpha << endl;
Mystring a {"frank"};
Mystring b {"frank"};
cout << (a==b) << endl; // true
cout << (a!=b) << endl; // false
b = "george";
cout << (a==b) << endl; // false
cout << (a!=b) << endl; // true
Mystring s1 {"FRANK"};
s1 = -s1;
cout << s1 << endl; // frank
return 0;
}
Mystring.cpp:
#include <iostream>
#include <cstring>
#include "Mystring.h"
// No-args constructor
Mystring::Mystring()
: str{nullptr} {
str = new char[1];
*str = '\0';
}
// Overloaded constructor
Mystring::Mystring(const char *s)
: str {nullptr} {
if (s==nullptr) {
str = new char[1];
*str = '\0';
} else {
str = new char[strlen(s)+1];
strcpy(str, s);
}
}
// Copy constructor
Mystring::Mystring(const Mystring &source)
: str{nullptr} {
str = new char[strlen(source.str)+ 1];
strcpy(str, source.str);
// std::cout << "Copy constructor used" << std::endl;
}
// Move constructor
Mystring::Mystring( Mystring &&source)
:str(source.str) {
source.str = nullptr;
// std::cout << "Move constructor used" << std::endl;
}
// Destructor
Mystring::~Mystring() {
delete [] str;
}
// Copy assignment
Mystring &Mystring::operator=(const Mystring &rhs) {
// std::cout << "Using copy assignment" << std::endl;
if (this == &rhs)
return *this;
delete [] str;
str = new char[strlen(rhs.str) + 1];
strcpy(str, rhs.str);
return *this;
}
// Move assignment
Mystring &Mystring::operator=( Mystring &&rhs) {
// std::cout << "Using move assignment" << std::endl;
if (this == &rhs)
return *this;
delete [] str;
str = rhs.str;
rhs.str = nullptr;
return *this;
}
// Display method
void Mystring::display() const {
std::cout << str << " : " << get_length() << std::endl;
}
// getters
int Mystring::get_length() const { return strlen(str); }
const char *Mystring::get_str() const { return str; }
// overloaded insertion operator
std::ostream &operator<<(std::ostream &os, const Mystring &rhs) {
os << rhs.str;
return os;
}
// overloaded extraction operator
std::istream &operator>>(std::istream &in, Mystring &rhs) {
char *buff = new char[1000];
in >> buff;
rhs = Mystring{buff};
delete [] buff;
return in;
}
Mystring Mystring::operator-() const{
char *buff = new char[std::strlen(str)+1];
std::strcpy(buff,str);
for (size_t i = 0; i < std::strlen(buff); i++)
{
buff[i] = std::tolower(buff[i]);
}
Mystring temp {buff};
delete [] buff;
return temp;
}
bool Mystring::operator==(const Mystring &rhs) const{
return strcmp(str,rhs.str);
}
bool Mystring::operator!=(const Mystring &rhs)const{
return !(strcmp(str,rhs.str));
}
Mystring.h:
#ifndef _MYSTRING_H_
#define _MYSTRING_H_
class Mystring
{
friend std::ostream &operator<<(std::ostream &os, const Mystring &rhs);
friend std::istream &operator>>(std::istream &in, Mystring &rhs);
private:
char *str; // pointer to a char[] that holds a C-style string
public:
Mystring(); // No-args constructor
Mystring(const char *s); // Overloaded constructor
Mystring(const Mystring &source); // Copy constructor
Mystring( Mystring &&source); // Move constructor
~Mystring(); // Destructor
Mystring &operator=(const Mystring &rhs); // Copy assignment
Mystring &operator=(Mystring &&rhs); // Move assignment
Mystring operator-() const;
bool operator==(const Mystring &rhs) const;
bool operator!=(const Mystring &rhs) const;
void display() const;
int get_length() const; // getters
const char *get_str() const;
};
#endif // _MYSTRING_H_