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.
Related
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 3 months ago.
Improve this question
This stack program runs correctly for given array size arr[5] or arr[10].But when i take size as input from the user cin>>n it says "Error: ‘cin’ does not name a type."
Here is that code:
#include <iostream>
using namespace std;
class Stack
{
private:
int n;
cin>>n;
int arr[n];
int top=-1;
public:
rest of the code
So i tried to take a random input for size of array but got this error when used cin to take input. I have used namespace std but still got the error Error: ‘cin’ does not name a type.
It's my first time asking question in stack overflow so ask me if you need more clarification.
There are two problems with this class definition
class Stack
{
private:
int n;
cin>>n;
int arr[n];
int top=-1;
//...
The first one is that you may not use statements that are not declarations
cin>>n;
And the second one is that variable length arrays are not a standard C++ feature.
You could define the class for example the following way
class Stack
{
private:
int n;
int *arr;
int top=-1;
//...
public:
Stack( int n ) : n ( n < 1 ? 1 : n ), arr( new int[n] )
{
}
//...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am new to C++ and have come across the const keyword. I looked it up online and read that The main use is to prevent the changing the value throughout the program. I have these two snippets below
const int size = 56;
Compared to using
int size = 56;
Why should I use one over the other? What is the idea behind using it.
Yes, you should make all variables const if you are never going to change its value after initialization. This prevents bugs where you accidentally change a value you're not supposed to. You seem to be aware of this already.
In addition, there is something that you might not be aware of, which is that making an int variable const also makes it a constant-expression, so long as the initializer itself is also a constant-expression, e.g. the int literal 56. This allows you to use it in contexts where you need a constant-expression, e.g. as the dimension of a static array:
const int size = 56;
int a[size]; // ok
int size = 56;
int a[size]; // error
In c++ CONST is used when you want to declare something which will not change its value throughout the program. But if you accidentally try to rewrite its value, the compiler will through an error. Most of the time it is recommended to declare a variable of int type so you can redefine its value when it's required. CONST is used when you want to declare a static array i.e.
const int a = 100;
int sampleAray[a];
And int is used when you, the value of the variable will be modified at some point i.e.
int a = 12;
int arr[4] = {11,22,33,554};
for (int i=0; i<4; i++){
if(arr[i]%2 == 0){
a+=arr[i];
}
}
When you have a const variable holding something that should not change and accidentally write code that modifies the variable anyway, you get a nice compiler error and can correct your mistake easily. If the variable is non-const, the compiler cannot help you and you now have a bug that may be hard to find.
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
I read on another stackoverflow post (variable length array error when passing array using template deduction) that the following should be possible:
#include <iostream>
int input() {
int a;
std::cin>>a;
return a;
}
int main()
{
const int b = input();
int sum[b];
std::begin(sum);
}
Except that it doesn't seem to work, I still get an similar error.
In function 'int main()':
16:17: error: no matching function for call to 'begin(int [b])'
16:17: note: candidates are:
Followed by information on possible templates it could fit.
You can use std::begin(sum) only when sum is regular array, not when it is a variable length array.
The following is OK.
const int b = 10;
int sum[b];
std::begin(sum);
In your case, b is not known at compile time. For arrays whose length are not known at compile time, it's better to use std::vector instead of relying on a compiler specific extension. The following is OK.
const int b = input(); // You can use int b, i.e. without the const, also.
std::vector<int> sum(b);
std::begin(sum);
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>)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Can someone help me with my program? I need to pass the three arrays into the function called calc_volts and then calculated the volts and then display the values. I keep getting errors that say "unreferenced local variable" or "undeclared idebtufier" for the variables; i, j, k, and volts.
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
double calc_volts(double, double, double, int);
int main()
{
const int max = 10;
int i; double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
int j; double volts[max];
int k; double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};
}
double calc_volts(double current[],double volts[], double resistance[], int max)
{
for (j = 0, j<max, j++)
volts[j] = current[i]*resistance[k];
return volts[j];
}
You have many problems:
Your function declaration is wrong:
double calc_volts(double, double, double, int);
It should be:
double calc_volts(double[], double[], double[], int);
You must invoke the function in order to use it:
int main()
{
const int max = 10;
double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
double volts[max];
double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};
calc_volts(current, volts, resistance, max); // call the function to execute it
}
The variables i, j and k do not exist inside calc_volts because they were declared inside main. Variables declared inside a function can only be used inside that function.
To fix the problem just put the declarations inside calc_volts.