user593301
user593301

Reputation: 913

C++ string array char questions

why when i try to compile this code:

TenStrings::TenStrings()
{
char str1[10] = "String 1";
char str2[10] = "String 2";
char str3[10] = "String 3";
char str4[10] = "String 4";
}
;

i get error: unused variable str1, str2, str 3, str4? I am trying to make a char array that will fit "string 1", "string 2", etc

Upvotes: 0

Views: 784

Answers (6)

Dan Breslau
Dan Breslau

Reputation: 11522

For the assignment that you're working on, you do not want to be declaring variables named str1, str2, etc. You want your TenStrings class to have a single data member (call it strings) which is an array of ten char pointers. This array would be initialized in your constructor.

You have spent so much time asking questions here, so much of which cover the same basic material. You would almost certainly be better off taking a break from StackOverflow, and using that freed-up time to study some of those tutorials that I've just linked to (or any others that you may have at your disposal.)

EDIT (responding to your comment): Here's an example. It won't solve your homework problem, but it should give you some hints. Good luck. (By the way, I haven't compiled this, but I think it compiles.)

class SimpleExample {
    public:
        SimpleExample();
        SimpleExample& operator += (const SimpleExample&);
        friend ostream& operator cout(ostream&, const SimpleExample&);

    private:
        int myData[5];
}

SimpleExample::SimpleExample()
{
    // Initialize a new SimpleExample instance. (Note that myData[i] is
    // the exact same thing as this->myData[i] or (*this).myData[i] . )
    for (int i = 0; i < 5; i++) {
         myData[i] = i;
}

SimpleExample& operator += (const SimpleExample& that)
{
    for (int i = 0; i < 5; i++) {
        myData[i] += that.myData[i];
    }

    return *this;
}

ostream& operator << (ostream& os, const SimpleExample& simp)
{
    for (int i = 0; i < 5; i++) {
        os << that.myData[i] << " ";
    }

    return os;
}

Upvotes: 0

jopasserat
jopasserat

Reputation: 5920

Just because you declare these variables but never use them later in your code (as far as we and the compiler can see).

I suppose you want to initialize these variables in your constructor to use them in other parts of your code.
To make this work, you should declare these variables as members. You'll then be able to initialize them without the "unused variable" complain.

Here is an example of member variables:

class TenStrings {
  private:
    char str1[10] = "String 1";
    char str2[10] = "String 2";
    char str3[10] = "String 3";
    char str4[10] = "String 4";


  public:
    TenStrings();
};

TenStrings::TenStrings()
  :str1("String 1"), str2("String 2"), str3("String 3"), str4("String 4"), 
{
}

This code declares your 4 variables as member of the TenStrings class and then initialize them before entering the body of the constructor.

Upvotes: 0

kohlehydrat
kohlehydrat

Reputation: 505

This is a warning, not an error. Just could (but should not) just ignore it.

Upvotes: 0

John
John

Reputation: 16007

Maybe it's a warning being treated as an error? You just instantiate the four strings but don't do anything with them.

Upvotes: 0

Yann Ramin
Yann Ramin

Reputation: 33177

The unused variable warnings simply point out that you have created a variable, but never actually did anything useful with it.

Currently, you are not doing anything with str1-4.

Upvotes: 2

Rob Kennedy
Rob Kennedy

Reputation: 163267

The compiler warns you that those variable are unused because you don't use them. The compiler is worried that you've left your code half-implemented — you declared a bunch of variables but haven't used them yet.

That's only an error if you've configured your compiler to treat warnings as errors.

Upvotes: 0

Related Questions