dynamic memory allocation inside class

Hi guys, trying to do some dynamic memory allocation inside a class but visual studio red underlines the 1300 in Sales john(1300, "John");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 #include <iostream>
using namespace std;
#include <cstring>

class Sales
{
public:
	Sales(double = 0.0, char* = "nobody");
	~Sales();
	void print();
private:
	double totalSales;
	char* name;
};
Sales::Sales(double a, char* n)
{
	totalSales = a;
	name = new char[strlen(n) + 1];
}
Sales::~Sales()
{
	delete [] name;
}
void Sales::print()
{
	cout << "Name:" << name << "\nTotal Sales : " << totalSales << endl;
}
int main()
{
	Sales john(1300, "John");
	john.print();
	return 0;
}
> but visual studio red underlines the 1300
red underlines is not an error message
compile your project and then check the build output
A non-const char* should not point to a string literal. This will give a warning in C++03, and will not compile in C++11 and beyond.

Change char* to const char* in your Sales constructor. The char* name in your class can stay the same. You also never copy over the actual characters in the string you allocate space for on line 18 (Hint: strcpy).

PS: Variable names like 'a' are usually not good, because they tell me nothing about what the variable means.
Last edited on
Visual Studio's Intellisense (the squiggly red line under 1300) is "misreporting" the int as being the problem. As Ganado points out the problem is your use of the string literal ("John") being passed to the ctor.

Two errors, one problem:
Error E0310 default argument of type "const char *" is incompatible with parameter of type "char *"
Error E0289 no instance of constructor "Sales::Sales" matches the argument list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstring>

class Sales
{
public:
   Sales(double = 0.0, const char* = "Nobody");
   ~Sales();
   void print();

private:
   double totalSales;
   char*  name;
};

int main()
{
   Sales john(1300, "John");

   john.print();
}

Sales::Sales(double a, const char* n)
{
   totalSales = a;

   name = new char[strlen(n) + 1];

   // gotta copy the const c-str to your data member c-str
   strcpy_s(name, strlen(n) + 1, n);
}

Sales::~Sales()
{
   delete[] name;
}

void Sales::print()
{
   std::cout << "Name: " << name << "\nTotal Sales: " << totalSales << '\n';
}

Why strcpy_s instead of strcpy? VS gets a bit anal about buffer overflows. The strcpy_s string function used to be one only provided by VC++, but with C11 is now part of the standard.
https://en.cppreference.com/w/c/string/byte/strcpy

And yes, VS 2019 apparently defaults to the C11 standard.

When writing C++ code you should prefer std::string over C strings. If you need a C string from a C++ string use std::string::c_str().
https://en.cppreference.com/w/cpp/string/basic_string/c_str
There's no need to use strcpy_s here as the allocated buffer is the correct size. If you really need to use c-style null-terminated strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstring>

class Sales
{
public:
	Sales(double = 0.0, const char* = "Nobody");
	~Sales();
	void print();

private:
	double totalSales {};
	char* name {};
};

Sales::Sales(double a, const char* n) : totalSales(a), name(strcpy(new char[strlen(n) + 1], n)) {}

Sales::~Sales()
{
	delete[] name;
}

void Sales::print()
{
	std::cout << "Name: " << name << "\nTotal Sales: " << totalSales << '\n';
}


int main()
{
	Sales john(1300, "John");

	john.print();
}

Last edited on
Topic archived. No new replies allowed.