0

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

1
  • 2
    That sounds like a warning rather than an error. The compiler is just letting you know that you have assigned a value to something which is never subsequently read. This usually indicates a mistake. Commented Feb 7, 2011 at 22:05

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

3 Comments

how do you do an array of ten char pointers? would this be right: char *string = {"string 1, "string 2",...}?
@user593301: Here's one way to do it: typedef char* charptr; charptr strings[10];. Now, see if you can do it without the typedef. And remember, this array is a data member of your TenStrings class, so you can not initialize it in the same place where you declare it. The initialization (what you're trying to do with = {"string 1, "string 2",...} would be done in the constructor.
btw..the constructors tutorial you gave me helped a lot. Im finally getting some output going
0

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.

2 Comments

thnk you. but is that a proper way of doing this if i want strings stored in array without using the <string> class?
If you want strings stored in an array, then declare an array of strings and store some there. You have no array of strings here. You have four arrays of char. If you don't want to use the <string> header, that's fine; you can still use C-style strings, but not the std::string class. That has no bearing on how to use arrays, though.
0

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

Comments

0

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

Comments

0

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.

1 Comment

how can i do this? declare them as members? can you give me an example thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.