Error: Initializing Argument 1 of [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've looked around and seen quite a few of these, but none of them provided the solution for my problem. I'm getting this compilation error with the following code:
THE ERROR:
THE CODE:
const int TOP_WORDS = 25;
...
void topWords(Hash t, string word, string topA[]);
int main()
{
...
Hash table1;
string word = "example";
string topWordsArr[TOP_WORDS];
table1.addItem(word);
topWords(table1, word, topWordsArr);
...
}
...
void topWords(Hash t, string word, string topA[])
{
int i = 0;
int tempCount = t.itemCount(word);
int tempCount2 = t.itemCount(topA[i]);
while (tempCount > tempCount2 && i < TOP_WORDS) {
i++;
tempCount2 = t.itemCount(topA[i]);
}
if (i > 0)
All the other posts I've seen about this error involved an incorrect syntax with declaring/passing the string array parameter, but I've double and triple checked it all and I'm certain it's correct; though I've been wrong before..

Using my crystal ball:
you're passing the Hash by value
this requires the copy constructor,
you don't have one (or it's botched, private or explicit)
So, take the Hash by reference
void topWords(Hash const& t, std::string const& word, std::string* topA);
Also,
string[] is not a type in C++
don't use using namespace std;
don't use raw arrays; use std::vector<std::string> (or std::array<std::string, N>)

Related

How do you compare strings as arguments? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to write a programm that reads the surname introduce and gives back the info of the student (birthdate, group, etc). The compiler does not recongize the == operator, I guess it doesnt know what to compare, either the address or the value?(I would appriciate an explanation)
I read similar cases and they suggest to use bool operator. As far as I know bool operator just returns true or false(I dont see the way to use it to print the students info)
Also I was considering usind char or using strcmp (srting functions for comparing).
void find_fam ( struct st student[], int length, string & fam)
{
for(int i=0; i<length; i++)
if ( student[i]== fam[i]){
cout << "Student:\n"<< student[i].imia << student[i].otchestvo << student[i].familia <<
student[i].gruppa << student[i].grozhdenia);
}
}
If you have a struct like this for example
struct st
{
std::string name;
int age;
};
Then you would want to compare the fam to that member of your struct
if (student[i].name == fam)
So now .name is the corresponding std::string that you are trying to compare. Otherwise you are trying to compare a std::string to a st struct.
In this example you've shown, you're comparing an entire instance of the struct to the string, which is invalid.
What you should do is compare the string attribute to the string you take in.
Assuming the family of the student is called family and is of type string:
if(student[i].family==fam)
{
//statements
}

I am studying C++, Can't I access reference by using pointer? (having an example) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Ok I have used C for 3 years.
My major is embedded system Engineering
I have had an interest in image processing just a few days ago.
So I am studying C++.
I want to change last character when I use copy constructor.
like these -> Cannon1 Cannon2 Cannon3 (Cannon is just name)
So I used pointer with While as I do in C.
Photon_Cannon::Photon_Cannon(int x, int y, char *cannon_name)
{
int i = 0;
hp = shield = 100;
coord_x = x;
coord_y = y;
damage = 20;
name = new char[strlen(cannon_name) + 1];
strcpy(name, cannon_name);
}
this is original code (this works normally)
and I attached some codes end of the section of copy constructor.
while (*name!=NULL) {
name++;
}
*name+=1;
but it doesn't work! :(
help me...
Try to use std::string instead of char*.
Photon_Cannon::Photon_Cannon(int x, int y, const std::string& cannon_name)
Change member variable name to std::string aswell, then you can easily assign string's like
name = cannon_name;
And then change last character
name[name.length() - 1] = '2';
Or
name.pop_back();
name += "2";
Don't forget to include string.
Or with char*
name[strlen(name) - 1] = '2';
Dont forget to check size of array/string, name.length() > 0), (int)strlen(name) > 1, ...
I would recommend you to read some c++ tutorial's like
C++ strings.
Iam new on stackoverflow, so i hope it helped you :).
This is not intended as answer, just as a side note to Filip's already given answer (and the comments there):
void f(std::string const& s)
This is the usual way to pass std::string objects, because it avoids creating unnecessary copies of (costing runtime and memory). If you actually want to modify the string (changes will be visible to the string object passed to f), leave away the const modifier.
Now here we have a special case:
void f(std::string const& s)
{
std::string ss(s); // creates a COPY!
// modify the copy ss...
}
Obviously, the copy is to be modified, but changes shall not be visible to the original string object passed to. If we don't need access to the original string any more (s), then and only then, it makes sense to pass the string by value:
void f(std::string /*const&*/ s) // creates the COPY already when passing
{
/* std::string ss(s); // creates a COPY! */ // <- don't need it any more
// modify the COPY s now...
}
Side note: Really leaving the const reference as comment as above (/*const&*/) would express (and should suffice for) that passing by value was done intentionally...

C++ error, compiler won't recognize string::push_back [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The function it has an issue with:
string encode (string message, string key) {
string code = "whatever";
string forst;
int num;
string::size_type begin = 0;
message = lower_and_strip(message);
for (char val : message) {
num = return_encoded_char(key, begin, val);
forst = to_string(num);
code.push_back(forst); //*******************************
}
return code;
}
The starred line is what it points to. The return_encoded_char function returns an integer.
The specific error is
proj05.cpp:68:23: error: no matching function for call to 'std::basic_string<char>::push_back(std::string&)' and points to the line I starred.
I initially just declared code without initializing it, but changing that didn't fix it. All the similar questions I could find had some other element to blame; I feel like this should be relatively straightforward, though obviously it's not since it isn't working.
I have #include <stream> and using std::to_string etc. I'm using -std=c++11 to compile it.
Help.
P.S. Using Geany on Linux.
Your code variable is a std::string. The std::string class doesn't have a push_back() method that takes another std::string as input. You should try with the += operator instead, which accepts a character or a string:
string encode (string message, string key) {
string code = "whatever";
string forst;
int num;
string::size_type begin = 0;
message = lower_and_strip(message);
for (char val : message) {
num = return_encoded_char(key, begin, val);
forst = to_string(num);
code += forst; //*******************************
}
return code;
}

no matching function call in C++, need fresh pair of eyes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am building a C++ project for fun and I have the following error: No matching function for call to 'loopAction' I have spent about 1.5 hours trying to figure this out and reading through Stack overflow about what could be the issue. I am hoping that an extra set of eyes will help me find the issue.
I have the following prototype in my program:
//Global Constants
const int ROWS = 100;
const int COLS = 100;
void loopAction(fstream &, int [][COLS], int, int, int);
In the above example, fstream is a file object, int [][] is a 2-d array, and the three last values are variables.
Variables that I have declared:
ifstream File;
File.open("deskInfo.txt");
int n, m;
char theValues[ROWS][COLS];
Here is the call to my function:
loopAction(File, theValues, ROWS, n, m);
And the actual function:
void loopAction(fstream &file, int values[][COLS], int rows, int n, int m){
char row;
for (int r = 0; r < n; r++){
for (int c = 0; c < m; c++){
file >> row;
values[r][c] = row;
}
}
}
Please let me know if more information is needed. The full error message is:
Semantic issue, No matching function for call to 'loopAction' =>
Candidate function not viable: no known conversion from 'ifstream' (aka 'basic_ifstream<char>') to 'fstream &' (aka 'basic_fstream<char> &') for 1st argument
theValues is typed as char[][] but the method signature accepts int[][].
A few other tips unrelated to your original question:
Use templated methods to prevent array-decay, which would help in this situation (see below)
Prefer const to #define
Be consistent in your naming and capitalization conventions: you're using the Title-cased File seemingly arbitarily compared to your other pascalCased identiifers.
Put parameter names in your method declarations
Array decay (how foo[N] turns to foo*) can be prevented by accepting an array type (with size) as a template argument:
template<typename TArray,int length>
void loopAction(fstream& file, TArray& values[length], size_t n, size_t m)
This way you don't need to use ROWS and COLS, you can use sizeof correctly.
I think the compiler error message says it all. The function expects an fstream, but you're passing an ifstream. ifstream does not derive from fstream. See http://www.cplusplus.com/reference/fstream/ifstream/ for some detail.

Why is my program giving ambiguous reference to wstring error? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why is the following code is giving this error:
reference to ‘wstring’ is ambiguous; i = wstring(ws);
I don't have any other function named wstring to cause the ambiguity.
int wstring(string &act)
{
int i=0;
vector<int> hash;
while(act[i++]!='\0')
if(act[i]=='#')
{
hash.push_back(i);
cout<<i<<endl;
}
return i;
}
int main()
{
int cases, i;
string ws;
cin>>cases;
while(cases--)
{
getline(cin, ws);
i = wstring(ws);
cout<<i;
}
return 0;
}
In the beginning of your code, do you have using namespace std?. Because if so, the call is ambiguous. You can fix it through:
i = ::wstring(ws);
namespace std has the same name as a typedef for std::basic_string<wchar_t> (i.e std::wstring). You have a name conflict because the compiler doesn't know if you meant wstring(ws) as a cast-expression to a std::wstring object or a function call. This is why, for one, using namespace std is not recommended, and why giving names to objects that have similar names in the global namesapces causes ambiguity.
I am assuming you also have at the top of your file
#include <string>
using namespace std;
This is where your ambiguous call to wstring exists. wstring is a type declared in string and the compiler can't tell if you mean the function wstring or if you want to create an object of type wstring and convert it to an int.