Const/String Problems

I'm working on a class Word. The objective of the main program is to
open a file (which contains a poem), put the file into a dynamic array
of pointers, which each point to one Word. The pointer to the main
array is wordArr, which is a double pointer. Each node in the array
points to a Word. So it looks like:
WordArr** -> (Word*[0] -> (Word1), Word*[1] -> (Word2), Word*[2] ->
(Word3), etc)

Here is words.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Word;
#include "words.h"
#include <iostream>
#include <string>
using namespace std;

string* ptr;
int len;

Word::Word(const string* word)
{
    len = word.size();
    ptr = new char [len + 1];
    strcpy (ptr, word.c_str());
}

Word::~Word()
{
    ptr = NULL;
    len = 0;
}

char Word::GetFirstLetter()
{
    return ptr[0];
}

char* Word::GetWord()
{
    return ptr.c_str();
}


Words.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

#ifndef WORD_H
#define WORD_H

class Word
{
private:
    string* ptr;
    int len;

public:
    Word(const string* word);
    ~Word();
    char GetFirstLetter();
    char* GetWord();
};

#endif 


I keep getting this error: "words.cpp:22: error: request for member
‘size’ in ‘word’, which is of non-class type ‘const std::string*’"
What does this mean? How can I fix this?

Any help is appreciated. Thank you!
you know you can just make a vector of strings right?
I have to use the .h file given, but I can edit it a little bit. I don't know how to make a vector of strings (or even what it is!), and the instructor wouldn't expect me to know that, so that's not really an option. Thanks for your suggestion, though.
Well, I see your dilemma, let me explain what it is (for future refrence). A vector is like an array, except it resizes itself to meet your needs. Since a string can be just a word, a vector of strings would be a vector of words.

Now about your problem: You said len = word.size(); but word is only a pointer you must put len = *word.size(); for it to to access the value of size.
Again here strcpy (ptr, word.c_str()); you must put the * signifying value like so: strcpy (ptr, *word.c_str());
word->size(); or (*word).size()
Why are you using a pointer in word? why don't just a string?
1
2
3
4
5
6
7
8
Word::Word(const string* word)
{
//....
    //ptr = new char [len + 1]; 
    ptr = new string; //ptr is a string pointer (not a char pointer)
    //strcpy (ptr, word.c_str()); 
    *ptr = *word;
}


what are those global variables in word.cpp?
Last edited on
haha, oops. Thanks for catching that ne555.
Topic archived. No new replies allowed.