2

Edit: I've reduced a majority of the errors with the std:: prefix on string declarations. However, there seems to be a problem with a few functions, particularly the definitions of those functions who have a string as a parameter.

Edit #2: Updated my code (I DID have the std:: prefix on my function declarations, but hadn't reflected it in my post). Please see the very bottom for errors displayed when I add the std:: prefix to string parameters in the problematic functions.


I have a header file movie.h with the following code (relevant code):

#include <string>

class Movie
{
    public:

        void addMovieName(std::string movie);
        void addLastName(std::string nameLast);
        void addFirstName(std::string nameFirst);

    private:

        string movieName,
               directorLastName,
               directorFirstName,
               directorFullName;
};

And an implementation file movie.cpp like this (relevant code):

#include "movie.h"

// addFirstName, addLastName, and addMovie name all do the same things
// so I'm only including one since they all generate the same error

void Movie::addFirstName(string nameFirst)
{
   directorFirstName = nameFirst.resize(10, ' ');
}

Upon compilation, I get the following errors:

g++ -c movie.cpp -o movie.o
movie.cpp:225: error: variable or field ‘addFirstName’ declared void
movie.cpp:225: error: ‘int Movie::addFirstName’ is not a static member of ‘class Movie’
movie.cpp:225: error: ‘string’ was not declared in this scope
movie.cpp:226: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:240: error: variable or field ‘addLastName’ declared void
movie.cpp:240: error: ‘int Movie::addLastName’ is not a static member of ‘class Movie’
movie.cpp:240: error: ‘string’ was not declared in this scope
movie.cpp:241: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:255: error: variable or field ‘addMovieName’ declared void
movie.cpp:255: error: ‘int Movie::addMovieName’ is not a static member of ‘class Movie’
movie.cpp:255: error: ‘string’ was not declared in this scope
movie.cpp:256: error: expected ‘,’ or ‘;’ before ‘{’ token
make: *** [movie.o] Error 1

Some of who have said I need to prepend std:: to the string parameters in the function definitions.

Upon doing this:

// adding std:: prefix
void Movie::addFirstName(std::string nameFirst)
{
   directorFirstName = nameFirst.resize(10, ' ');
}

I get the following errors. Note that I only changed it for a single function. The first errors I don't understand, whereas the rest remain the same as before.

g++ -c movie.cpp -o movie.o
movie.cpp: In member function ‘void Movie::addFirstName(std::string)’:
movie.cpp:227: error: no match for ‘operator=’ in ‘((Movie*)this)->Movie::directorFirstName = nameFirst.std::basic_string<_CharT, _Traits, _Alloc>::resize [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](10u, 32)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:485: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:493: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:504: note:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
movie.cpp: At global scope:
movie.cpp:240: error: variable or field ‘addLastName’ declared void
movie.cpp:240: error: ‘int Movie::addLastName’ is not a static member of ‘class Movie’
movie.cpp:240: error: ‘string’ was not declared in this scope
movie.cpp:241: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:255: error: variable or field ‘addMovieName’ declared void
movie.cpp:255: error: ‘int Movie::addMovieName’ is not a static member of ‘class Movie’
movie.cpp:255: error: ‘string’ was not declared in this scope
movie.cpp:256: error: expected ‘,’ or ‘;’ before ‘{’ token
make: *** [movie.o] Error 1
skippr
  • 2,656
  • 3
  • 24
  • 39
  • What error do you get when using std::string? Also, open to see how a string is declared. Sometimes, "string" is an alias to basic_string<> depending on what STL package you're using. – Eddie Paz Mar 22 '12 at 05:20
  • you need `using namespace std;`, dude. – J-16 SDiZ Mar 22 '12 at 05:25
  • How are you learning C++? These are very basic principles which you are missing. – Jesse Good Mar 22 '12 at 05:26
  • You actually do need to specify `std::string` every place you have just `string` or take @J-16SDiZ's suggestion. In addition, why are you using dynamic allocation for those `int` and `float` variables? It's an uncommon practice. – Blastfurnace Mar 22 '12 at 05:31
  • `directorFirstName = nameFirst.resize(10, ' ');` resize returns void so this will fail also. – Jesse Good Mar 22 '12 at 05:32
  • seems someone beat me to it in the comments whilst i was typing i guess... – Aleks Mar 22 '12 at 05:33
  • @Jesse well definitely not from people like you. If that was the case, I'd be depressed that I make mistakes and never realize failure is the key to success :) – skippr Mar 22 '12 at 05:34
  • @sunday: Sorry if I offended you, but the question gave me the impression that a lack of effort was put in figuring out the answer yourself (making mistakes is fine of course). – Jesse Good Mar 22 '12 at 05:43
  • @Jesse hopefully I've redeemed myself through my responsiveness. I'll admit that at first I was intimidated by the amount of errors that came out and had no idea what to do (thus why I was posting so much code). But I fixed those, and updated my post to become more specific. I'm definitely not looking to be a leech, and be spoon fed. And no, you didn't offend me one bit. :) – skippr Mar 22 '12 at 05:47
  • @sunday: Well, changing all `string` to `std::string` in a total of 10 locations and changing `directorFirstName = nameFirst.resize(10, ' ');` to `directorFirstName = nameFirst;` (I'm not sure of your intention here) fixed all the errors for me. – Jesse Good Mar 22 '12 at 05:55
  • resizing here is a requirement. Seems to be a problem in the way I'm implementing resize. Thoughts? – skippr Mar 22 '12 at 05:59
  • Copy first resize after? – Aleks Mar 22 '12 at 06:12
  • @Aleks I did `directorFirstName = nameFirst` then `directorFirstName.resize(/* blah */)` and it returns the same errors. Do I need to include some prefix to `resize` as well? – skippr Mar 22 '12 at 06:15
  • @sunday is the error log in the post current? I see nothing related to resize so it may be something else. – Aleks Mar 22 '12 at 06:29
  • @Aleks Yes, it is all current. I've played around with resize, and even removing it completely doesn't remove the errors. So it doesn't have to do anything with that after all. – skippr Mar 22 '12 at 06:31
  • @sunday hmm, have you appended std:: on all of the places including string? I have seen this before when there was an unknown type in the parameters. Are you using the terminal or an IDE? A capable IDE could probably find what's missing as something might be. – Aleks Mar 22 '12 at 07:46
  • The example code after your most recent edit still doesn't have `std::string` for the _variable_ and _function parameter_ declarations. Did you fix these in your actual code? – Blastfurnace Mar 22 '12 at 08:44

3 Answers3

2

You have not specified the correct namespace for string, string is a member of the std namespace. In this case you need to write std::string since I see no "using namespace std;" in your code.

replace:

string movieName,
       directorLastName,
       directorFirstName,
       directorFullName;

with:

std::string movieName,
            directorLastName,
            directorFirstName,
            directorFullName;
Aleks
  • 531
  • 2
  • 8
  • This was indeed one major fix. I was in the process of editing my post when you wrote this. – skippr Mar 22 '12 at 05:35
  • Yea, i looked a bit more into it and saw some other major problems, not sure why the pointers slipped my mind... – Aleks Mar 22 '12 at 05:38
0

Why are all your private fields pointers? Storing plain ints and floats as fields is perfectly okay. Moreover, you don't need to "initialize" strings in your constructor. If you don't, their default constructors will be called automatically (strings will be empty). And why do you pad strings? Even if you need it when displaying, pad them there.

Rev
  • 9
  • 1
  • 3
  • 1
    You seem to miss a lot and have (or you will) problems with many other things. Just wanted to point out things that are wrong in your code (even if they work.. for now). – Rev Mar 22 '12 at 05:48
  • This is a college course assignment. The `clear()` statements are to "clear any residual data out of the string-type attributes," and were a requirement of the lab. Dynamically allocated variables are moreso for practice, and not necessity. The resizing is in response to function criteria defined within the lab specs. – skippr Mar 22 '12 at 05:56
  • @sunday: I too wonder why you're using pointers and dynamic memory allocation for simple `int` and `float` variables. Manual resource management like that is unusual and error prone. Are you aware of the [The Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)? – Blastfurnace Mar 22 '12 at 05:56
  • @sunday: Note that you have `clear()` statements in the __constructor__. There is no "residual" data to clear when you are __constructing__ the `Movie` objects. – Blastfurnace Mar 22 '12 at 06:01
0

No one has answered the question yet, so I figured I'd just put it here myself since I figured it out. Basically, my solution was:

Use using std::string in header and implementation files.


Secondly, the declaration:

stringOne = stringTwo.resize(some_number, ' ');

...fails because resize() is a void returning function. Replacing that with two separate statements; namely:

stringOne = stringTwo;
stringOne.resize(/* blah */);

...solves the rest of the errors. Credit to @Jesse for mentioning this in the comments.

skippr
  • 2,656
  • 3
  • 24
  • 39