4 Replies - 2947 Views - Last Post: 01 January 2008 - 10:28 PM Rate Topic: -----

#1 Max_Payne  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 19-October 07

Preamble initialization of strcpy_s ?

Posted 01 January 2008 - 04:09 PM

Question:
I'm getting the error i posted below. I know there's another way to initialize strcpy_s in the preamble but i forgot how.

Code:
#pragma once

class Pizza
{
public:
	Pizza (const char top[] = "cheese", const int size = 12, const double pr = 8.99)
		: strcpy_s (_topping ,top), _diameter (size), _price (pr)
	{}

	~Pizza () {}

	void setValues ();
	void displayValues ();

private:
	char	_topping [20];
	int	 _diameter;
	double  _price;
};



Error:
error C2614: 'Pizza' : illegal member initialization: 'strcpy_s' is not a base or member

This post has been edited by Max_Payne: 01 January 2008 - 04:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Preamble initialization of strcpy_s ?

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Preamble initialization of strcpy_s ?

Posted 01 January 2008 - 05:29 PM

strcpy_s is used as:

strcpy_s(variable,element_to_copy);

for more:
http://msdn2.microso...da9(VS.80).aspx
Was This Post Helpful? 0
  • +
  • -

#3 Max_Payne  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 19-October 07

Re: Preamble initialization of strcpy_s ?

Posted 01 January 2008 - 06:43 PM

I'm well aware of how it's used. I asked how to initialize it in the constructors preamble?
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Preamble initialization of strcpy_s ?

Posted 01 January 2008 - 07:22 PM

it can be initialized as:
strcpy_s (_topping, sizeof(_topping) ,top)
as well, but i think it may be an SDK issue, i had problems with VC++ until i updated mine past the 2001 Fall release.

I believe the _s methods are not standard either, and only available in VC++, you would probably be better using sprintf, or some other substitute.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Preamble initialization of strcpy_s ?

Posted 01 January 2008 - 10:28 PM

You can't use strcpy_s or any other function function in an initialization list. As the error tells you, initialization lists do two primary things... it initializes parent/base class' through a constructor call or defines a default value for a data member. Since using strcpy_s is not really initialization, it is calling a separate function, it is not allowed. You will have to move this initialization process of calling a function to the body of the constructor instead.

Using strcpy_s would be like calling your own function "doSomething()" as far as the compiler is concerned and not allowed in the language.

Hopefully that answers the question to your satisfaction.

"At DIC we be initialization list code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1