-3
const int fileLength = fileContent.length();
    char test[1000];
    for (int p = 0; p < fileLength; ++p){
        test[p].append(fileContent[p]); // Error: expression must have class type
    };

I'm attempting to append the characters of a text file into an array which i've created. Though I'm getting the error " expression must have class type ". Tried googling this error to no avail.

Need help
  • 1
  • 1
  • 1
  • 4
    Possible duplicate of [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – BoBTFish Dec 22 '15 at 13:44
  • If you want to use 'append', why do you use a char array instead of a string or vector? – DrDonut Dec 22 '15 at 13:47

2 Answers2

5

test is an array of char. test[p] is a char. char does not have any members. In particular, it does not have an append member.

You probably want to make test a std::vector<char>

    const auto fileLength = fileContent.length();
    std::vector<char> test;
    for (const auto ch : fileContent)
    {
        test.push_back(ch);
    }

or even:

    std::vector<char> test( fileContent.begin(), fileContent.end() );

if you then really need to treat the test as an array (because you are interfacing to some C function for example), then use:

    char* test_pointer = &*test.begin();

If you want to use it as a nul-terminated string, then you should probably use std::string instead, and get the pointer with test.c_str().

0

char array does not have any member function by the name append .However , std::string does have one member function named append like below :

string& append (const char* s, size_t n);

I think you by mistake have used char array instead of std::string . std::string will resolve this problem like below :

const int fileLength = fileContent.length();
    string test;
    for (int p = 0; p < fileLength; ++p){
        test.append(fileContent[p],1); // Error: expression must have class type
    };

Better way would be string test(fileContent) .You can access test just like a array .Refer string class for more details .

SACHIN GOYAL
  • 955
  • 6
  • 19