Question:
Can i initialize char arrays using preamble initialization ?

Code:

#prag...
#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

Recommended Answers

All 11 Replies

>Can i initialize char arrays using preamble initialization ?
That's the first time I've heard that term, but I'll assume you're asking if you can use strcpy_s in the way you've attempted. Since you received an error, I'm wondering why you're even asking the question because it's obviously not allowed. But to answer your question: no, you can't.

Ok so you don't know how. 0_o

There's another way to initialize strcpy_s in the preamble but i forgot how.

>Can i initialize char arrays using preamble initialization ?
That's the first time I've heard that term

Initialization in the preamble means initialization by list.
ej:

World (int id)
        : _identifier (id), _matter (_identifier)

>Ok so you don't know how. 0_o
You didn't ask how, you asked if you can.

>There's another way to initialize strcpy_s in the preamble but i forgot how.
You're not initializing strcpy_s, you're initializing an array. However, arrays are aggregate types that cannot be used in initialization lists. You're also required to specify either a member of the class or a base class constructor, which rules out arbitrary functions such as strcpy_s. The answer to the question you asked is no, you cannot do this, and the answer to the question you mean to ask is move the function call to the constructor body:

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

>Initialization in the preamble means initialization by list.
Thank you. It's a good idea to define non-standard terminology for those of us who prefer to use standard terminology.

>You didn't do anything here. But what matters is that you tried.
Okay, in that case I won't bother trying to help you anymore.

>PS: Preamble is the standard terminology. It's Ok that you didn't know, no ones perfect.
Standard terminology is defined in the standard document. "Preamble" isn't in it (seriously, grep the document and the word doesn't exist), but it's okay that you didn't know. No one's perfect. However, your pompous attitude isn't okay, and for that you've earned a place in my kill file.

>http://technet.microsoft.com/en-us/l...e_members.aspx
Microsoft doesn't define C++, and that link doesn't appear to have anything to do with C++ either.

*plonk*

Don't bother. I have already found a way to do it. Seriously you need to check your c++ book reference more often.

You said that you never heard of the term *Preamble*. It doesn't matter if the link i gave you was for C++ or not, the term exist.

Really it's ok you don't have to feel bad no one's perfect. :yawn:

commented: People don't like smart a.r.s.e.s, especially when they're wrong. -2

>I have already found a way to do it. Seriously you
>need to check your c++ book reference more often.
Oh, please show us the code then. If you're going to give me that kind of attitude, you can at least back it up with a legitimate solution. I'm certainly willing to learn something new, even if it's from a jerk such as yourself.

>It doesn't matter if the link i gave you was for C++ or not, the term exist.
Well, if you remove the restriction that the term has to apply to C++, then I suppose you can argue the existence of any term. :icon_rolleyes:

a jerk such as yourself.

That's the first time I've heard that term, but I'll assume you're asking if you can use strcpy_s in the way you've attempted. Since you received an error, I'm wondering why you're even asking the question because it's obviously not allowed. But to answer your question: no, you can't.

When you act like a nerdy know it all with an attitude you shouldn't expect anyone to treat you nicely. It's simple human behavior. Oh my bad i guess you are not familiar with that term either. :icon_rolleyes:

@Max_Payne
You can not initialize arrays the way you are trying. However, you have exposed a good problem. How to initialize a constant string inside a class?
I have been facing these kinds of problems during my carrier and every time I implement a work around which suits to my requirements.

Look at the following implementation. I don’t know if it can help you but this is something you can try.

class MyClass {
	class MyStr {
	public:
		char	m_szArray[100] ;
		MyStr(const char *szPtr) {
			strcpy(m_szArray,szPtr);
		}
	};
	int		m_iInt;
	const MyStr m_MyStr ;
public:
	MyClass(int i, const char *szPtr):m_iInt(i),m_MyStr(szPtr)
	{

	}
};

You may like to overload couple of operators in MyStr class to make it friendly.

>You can not initialize arrays the way you are trying.
Oh, but you can. The OP claims to have done it, but I notice that he still hasn't provided proof of that claim. Apparently arrogance and snide comments are a viable alternative to technical proof.

>When you act like a nerdy know it all with an attitude
>you shouldn't expect anyone to treat you nicely.
Your compiler answered your question. I also answered your question. You felt the need to look down your nose at me and act like a pompous ass. If you want to throw around quotes, tell me who has more of a nerdy know it all attitude?

Ok so you don't know how. 0_o

There's another way to initialize strcpy_s in the preamble but i forgot how.

You didn't do anything here. But what matters is that you tried.

It's Ok that you didn't know, no ones perfect.

Seriously you need to check your c++ book reference more often.

It doesn't matter if the link i gave you was for C++ or not, the term exist.

Really it's ok you don't have to feel bad no one's perfect.

How about a little wager? If you provide a valid example of the solution you claim to have, I'll apologize for being a nerdy know it all with an attitude. If you fail to provide a valid example, you admit that I was right and your response to my correct answer was completely uncalled for. That seems fair enough, though I have the feeling you'll simply choose to attack me again because you're unwilling to swallow your pride and admit you don't know everything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.