# whats wrong with this code?

## Recommended Posts

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

string::size_type laengde(const vector<string>& v)
{
string::size_type maxlen = 0;
for(vector<string>::size_type i = 0; i != v.size; ++i)
maxlen = max(maxlen, v[i].size());

return maxlen;
}

vector<string> frame(const vector<string>& v)
{
vector<string> ret;
string::size_type maxlen = laengde(v);
string border(maxlen + 4, '*');

ret.push_back(border);

for (vector<string::size_type i = 0; i != v.size(); ++i)
{
ret.push_back("* " + v[i] + string(maxlen - v[i].size(), ' ') + " *");
}

ret.push_back(border);
return ret;
}

int main()
{
string v;

cout << "fortael mig lidt om dig selv: ";
string >> v;

cout << endl << frame(v) << endl;
return 0;
}


these is my errors:
Quote:
 Error 1 error C3867: 'std::vector<_Ty>::size': function call missing argument list; use '&std::vector<_Ty>::size' to create a pointer to member c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 11 Error 2 error C2446: '!=' : no conversion from 'unsigned int (__thiscall std::vector<_Ty>::* )(void) const' to 'unsigned int' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 11 Error 3 error C2040: '!=' : 'unsigned int' differs in levels of indirection from 'unsigned int (__thiscall std::vector<_Ty>::* )(void) const' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 11

##### Share on other sites
Add parentheses to the call to size in the condition of the for loop.

##### Share on other sites
Quote:
 Error 1 error C3867: 'std::vector<_Ty>::size': function call missing argument list; use '&std::vector<_Ty>::size' to create a pointer to member c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 11

Quote:
 'std::vector<_Ty>::size': function call missing argument list; use '&std::vector<_Ty>::size' to create a pointer to member

Then, find about what he's talking about: it mentions the "size" member function of a vector. You have one in "v.size". It also says that the argument list is missing.

The solution is simple: add the ().

##### Share on other sites
thanks! i must had overlooked it.. :(

it now tells me that i have a problem in the for loop in this function:

vector<string> frame(const vector<string>& v){ vector<string> ret; string::size_type maxlen = laengde(v); string border(maxlen + 4, '*');  ret.push_back(border);  for (vector<string::size_type i = 0; i != v.size(); ++i)  {    ret.push_back("* " + v[i] + string(maxlen - v[i].size(), ' ') + " *");  }    ret.push_back(border);  return ret;}

errors:
Quote:
 Error 1 error C2146: syntax error : missing ',' before identifier 'i' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 24Error 2 error C2065: 'i' : undeclared identifier c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 24Error 3 error C2143: syntax error : missing ',' before ';' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 24

sorry for me seeming helpless, but ive been trying to figure this out all day :(

##### Share on other sites
for (vector<string::size_type i = 0; i != v.size(); ++i)

You are missing a closing '>' after 'string', unless that is a forum typo.

for (vector<string>::size_type i = 0; i != v.size(); ++i)

##### Share on other sites
Quote:
 Error 1 error C2146: syntax error : missing ',' before identifier 'i' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 24

Clean up the error:

Quote:
 missing ',' before identifier 'i'

So, find the identifier i on line 24:

for (vector<string::size_type i = 0; i != v.size(); ++i)

If necessary, split it into multiple lines (one i per line). However, it isn't necessary here, as an identifier can always be safely preceded by ; or ; ++. So, the problem is with:

vector<string::size_type i

Which only takes a few seconds to notice that the closing > of the vector template is missing.

##### Share on other sites
i cant help making pathetic laughter at myself >_<x100

alright. I strike back with a (hopefully) not as dum question as previus.

how do i 'convert' a string into a vector?

#include <iostream>#include <vector>#include <string>#include <cctype>using namespace std;string::size_type laengde(const vector<string>& s){ string::size_type maxlen = 0; for(vector<string>::size_type i = 0; i != s.size(); ++i) 	maxlen = max(maxlen, s[i].size());    return maxlen;}vector<string> frame(const vector<string>& s){ vector<string> ret; string::size_type maxlen = laengde(s); string border(maxlen + 4, '*');  ret.push_back(border);  for (vector<string>::size_type i = 0; i != s.size(); ++i)  {    ret.push_back("* " + s[i] + string(maxlen - s[i].size(), ' ') + " *");  }    ret.push_back(border);  return ret;}  int main(){  cout << "fortael mig lidt om dig selv: ";string s;while (getline (cin, s)){ vector<string> v = frame(s); for (vector<string>::size_type i = 0; i != v.size(); ++i)  cout << endl << frame(s) << endl; }return 0;}

errors: (pointing to the main(), in the 'vector<string> v = frame(s);' line
Quote:
 Error 1 error C2664: 'frame' : cannot convert parameter 1 from 'std::string' to 'const std::vector<_Ty> &' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 42Error 2 error C2664: 'frame' : cannot convert parameter 1 from 'std::string' to 'const std::vector<_Ty> &' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 44

##### Share on other sites
Sorry, it's still just as dumb. The compiler error literally explains to you what the problem is. It's not like you encountered some unfathomable template trickery that's gone horribly wrong.

Quote:
 Error 1 error C2664: 'frame' : cannot convert parameter 1 from 'std::string' to 'const std::vector<_Ty> &' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 42

Cleaned up:

Quote:
 'frame' : cannot convert parameter 1 from 'std::string' to 'const std::vector<_Ty> &'

It says that the argument 1 of function frame in your code is of type std::string, but function frame expects an argument of type vector, and it could not convert from one to another. In short, you've provided an incorrect type of argument.

##### Share on other sites
i know that, im sorry for not explaining probaly, but i dont know how to 'convert' it. i mean.. they are both vector<string>..

no matter what changes i make in the argument/returntype and so on it gives me a million new errors. im freaking out! i ask you, please show me what im doing wrong.

##### Share on other sites
Quote:
 Original post by MadsGustafi know that, im sorry for not explaining probaly, but i dont know how to 'convert' it.

This isn't really a relevant question here. This particular error is not a minor compiler nitpick (such as a missing '>', an unclosed comment, or some other stupid error), it's telling you that you said one thing when declaring frame, and you're now saying another thing when using frame. You're being inconsistent with yourself, which is a clear sign that you are confused about your own code (which is a dangerous thing). So, the question to be asked here is not about the conversion, but about why you're being inconsistent in the first places. Two simple questions, in fact:

Question 1: why does frame take a vector of strings as argument? You chose to wrote it that way, so why?

Question 2: why are you trying to call frame on a line that was just read from standard input?

Asking these questions should reveal that you either incorrectly designed frame (so it should take a string as argument, instead of a vector of strings), or that you are incorrectly using it, and you should be calling another function, or passing another argument.

##### Share on other sites
thanks! i see i was making a wrong call.
changed into:

for (vector<string>::size_type i = 0; i != v.size(); ++i)
cout << v[i] << endl;

That converting thing is stil bugging me. I want to say that you are very helpful, and that im currently very annoying with all these questions. but no matter how much i try, and what return types/parametertypes i change, i still gets alot of problems. Can you by any chance fix it in a code blok? i believe that would make me understand it. Im just currently too confused to understand any of your hints, even thou i believe they are obvious..

##### Share on other sites
After a few minute of reading I think I understand what you WANT to do. You want to make an ASCII game of some type so that it appears like text graphics? And you want to make a 'frame' that's basically just the screen?

In any case, it's hard to tell you what you actually want to do. If frame always accepts a string, then just change this:

vector<string> frame(const vector<string>& v)

to this:

vector<string> frame(const string& s)

I'd offer more advice, but not knowing what you're actually trying to do I can't tell you everything you're doing wrong to accomplish that.

##### Share on other sites
thank you for your reply, but that just gives me these errors, that i have NO idea how to solve..

Quote:
 Error 1 error C2664: 'laengde' : cannot convert parameter 1 from 'const std::string' to 'const std::vector<_Ty> &' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 19Error 2 error C2039: 'size' : is not a member of 'System::SByte' c:\Documents and Settings\mads\Dokumenter\Visual Studio 2005\Projects\dasdfg\dasdfg\da.cpp 26

im trying to make it so, people can write a text, and then it will be frammed inside those '*'s.

heres the code btw:
#include <iostream>#include <vector>#include <string>#include <cctype>using namespace std;string::size_type laengde(const vector<string>& s){ string::size_type maxlen = 0; for(vector<string>::size_type i = 0; i != s.size(); ++i) 	maxlen = max(maxlen, s[i].size());    return maxlen;}vector<string> frame( const string& s){ vector<string> ret; string::size_type maxlen = laengde(s); string border(maxlen + 4, '*');  ret.push_back(border);  for (vector<string>::size_type i = 0; i != s.size(); ++i)  {    ret.push_back("* " + s[i] + string(maxlen - s[i].size(), ' ') + " *");  }    ret.push_back(border);  return ret;}  int main(){  cout << "fortael mig lidt om dig selv: ";string s;while (getline (cin, s)){ vector<string> v = frame(s);  for (vector<string>::size_type i = 0; i != v.size(); ++i)  cout << v[i] << endl; }return 0;}[/source}

##### Share on other sites
a vector<string> is not a string. vector<string> is an array of string.

laengde(const vector<string>& v)

and you call it:

laengde(s), where s is just a string, not vector<string>

A quick solution: make a dummy vector<string> before calling laengde, and insert your string s there.

A long solution: Rethink your design. If you pass a string to a function that accepts vector<string>, something is wrong with your design. laengde() finds the longest string in the array, and but you only have one string.

##### Share on other sites
If you don't try to listen to the posters advice you'll never learn. Several times you were shown how to read an error message, and when you look at the only important part the solution becomes obvious.

Your consistant problem seems to be that your passing the wrong parameters. Why do you need a vector of strings when all you intend to pass is a string? How hard is it to remember how your own code works? Especially in such a small program?

P.S. If I seem angry or harsh, its because I get agitated when people as other people to do their work for them. Its not cool.

##### Share on other sites
It will help you a lot, I think, if you add comments to your code that explain the intention of what you're doing. What is each function supposed to achieve? What does each parameter mean in that conceptual context?

Remember, at the end of the day, code is just an expression of some idea you've got about the way you want the computer to behave. You can also express those ideas in plain english (or whatever your native language is) which is easier for you to understand.

##### Share on other sites
Ah, now knowing what you're trying to do I can offer some better advice. There are a few things you have to consider before writing the code such as what happens if the text wraps from one line to the next? What happens if the text has new-lines embedded in it? I'm GUESSING you want there to be a possibility of more than one string, due to your 'length' function. I'll write some psuedo code and if you don't get it after that, I can't really think of a way to help you.

1)read in a line of text, put in vector of strings. Call this vector something descriptive, like LinesOfText

2)repeat 1 until a stop condition is met(maybe the user must enter "quit" by itself. Maybe you ask for 5 strings total. Maybe just get 1 string and go to step 3. Up to you)

3)Find the line of text that is longest in the VECTOR. Get the number of characters in this string and call it Length. (The code of yours is mostly correct, you just call the function with a STRING, not a VECTOR. This is like asking for a barrel of apples and giving just ONE apple.)

4)If you have N lines of text, then you need a new vector with N + 2 lines of text. Let's call this new vector TextBox

5)In the first and last rows of TextBox, assign a string with Length + 4 *s

6)For each line of text in LinesOfText, assign to appropriate spot in TextBox with a star in the front and back

7)Print each line of text in TextBox

8)Repeat program as needed

Pay attention to data types!

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627638
• Total Posts
2978330

• 10
• 12
• 22
• 13
• 34