VC++ error C2146: syntax error : missing ')' before identifier 'pFirst' - c++

I am new to C++, but I do pretty well at C# and Java. I am trying to do some basic exercise here:
// HelloWorld.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
bool isSame(int[] pFirst, int[] pSecond, int pLength) {
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
That is my entire file, but VC++ (I am using VS 2013) keeps complaining:
1 IntelliSense: expected a ')'
at the isSame declaration line.
What do I do wrong? I am writing a function to compare if two arrays are containing the same values, and here is the solution:
bool ArrayEq( int A1[], int A2[], int size ) {
for (int k=0; k<size; k++) {
if (A1[k] != A2[k]) return false;
}
return true;
}

The correct syntax would be int pFirst[].
But first of all this function already exists in the standard library, and second
std::vector overloads operator ==.
Both should be prefered over handcrafted code and std::vector is better than a C-style array.

Related

What is wrong with this C++ main method driver to run this method?

I'm doing C++ this semester (just finished learning Java) and using Codewars to get some practice (this has been my most effective way of practicing but it only requires the method not the driver) with my C++ coding but unsure how to create the driver for this method so I can practice in my IDE.
What am I missing here in the main so it runs in my IDE? The solution (not my code) works but my main method doesn't drive it.
#include <iostream>
#include <cctype>
#include <unordered_set>
int main(int argc, char *argv[]) {
using namespace std;
cout << is_isogram("Dermatoglyphics"); //error: use of undeclared identifier 'is_isogram'
}
bool is_isogram(std::string str) { //warning: Function is never used
std::unordered_set<char> char_set;
for (const auto &c : str) {
auto c_lower = std::tolower(c);
if (char_set.count(c_lower) == 0) char_set.insert(c_lower);
else return false;
}
return true;
}
At the point where you try to use is_isogram() in main(), it has not actually been declared yet, hence the "use of undeclared identifier" (a).
You can solve this in one of two ways, the first being to declare it with a function prototype before use, such as with:
bool is_isogram(std::string); # declare
int main(int argc, char *argv[]) { # define
// main stuff
}
bool is_isogram(std::string str) { # define
// is_isogram stuff
}
And the second is to swap is_isogram() and main() so that the declaration is done as part of the definition (defining something implicitly declares it):
bool is_isogram(std::string str) { # define
// is_isogram stuff
}
int main(int argc, char *argv[]) { # define
// main stuff
}
(a) The second reported issue, "function not used", is almost certainly just a side-effect of the first. Because the compiler couldn't do anything with the is_isogram() call from main(), it just throws it away after reporting the error. As a result, there is now no code that calls your function when you define it.

Why did I get rid of an error (reference to " " is ambiguous) just by removing using namespace std; in c++?

I was writing a recursive function (in C++) and instead of putting the arrays as parameters of function I made them global.
When i used those global arrays in my recursive function I got this following error:
"error: reference to "(name of array)" is ambiguous"
My Code:
#include<bits/stdc++.h>
using namespace std;
#define int long long int
int left[100],rit[100],col[100],ans=0; // Global arrays
char a[10][10];
int ways(int x,int res[][100]) // recursive function
{
if(x==9){ans++;}
for(int y=1;y<=8;y++)
{
if(res[x][y])continue;
if(col[y] || left[x+y] || rit[y-x+8])continue; //"error: reference to 'left' is ambiguous"
col[y]=left[x+y]=rit[y-x+8]=1;
ways(x+1,res);
col[y]=left[x+y]=rit[y-x+8]=0;
}
return ans;
}
int32_t main()
{
int res[100][100]={0};
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
{
cin>>a[i][j];
if(a[i][j]=='*'){res[i][j]=1;}
}
}
cout<<ways(1,res);
}
Surprisingly, I got rid of this error message when I removed using namespace std;
and my code compiled successfully with no error message.
My successfully compiled code:
#include<bits/stdc++.h>
#define int long long int
int left[100],rit[100],col[100],ans=0;
char a[10][10];
int ways(int x,int res[][100])
{
if(x==9){ans++;}
for(int y=1;y<=8;y++)
{
if(res[x][y])continue;
if(col[y] || left[x+y] || rit[y-x+8])continue;
col[y]=left[x+y]=rit[y-x+8]=1;
ways(x+1,res);
col[y]=left[x+y]=rit[y-x+8]=0;
}
return ans;
}
int32_t main()
{
int res[100][100]={0};
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
{
std::cin>>a[i][j];
if(a[i][j]=='*'){res[i][j]=1;}
}
}
std::cout<<ways(1,res);
}
My Question is:
why did I get rid of this error by just simply removing using namespace std;?
This is because left is the manipulator defined in C++ under namespace std.As,you are using it in your program as a global variable,it causes ambiguity.
If you rename left to lef(or any other name that is not keyword or manipulator)in your program,your program works perfectly.
Check this link to know about left manipulator:
http://www.cplusplus.com/reference/ios/left
Hope it helps!!
That's one of the reasons why you should not use using namespace std
The complete error is:
error: reference to ‘left’ is ambiguous
note: candidates are: ‘std::ios_base& std::left(std::ios_base&)’
left(ios_base& __base)
^~~~
…
int left [100]
int left[100],rit[100],col[100],ans=0; // Global arrays
^~~~
Because of using namespace std the compiler is not able to know if you mean std::left(std::ios_base&) or int left[100]. Without using namespace std it is not ambiguous anymore, because then std::left(std::ios_base&) has to be referenced with the std:: before it.

Why am I getting a <function> is not a member of <class>? I was trying to pass a string and return from it

I am trying to pass a string to a function, which is going to be sorted. Then to have the sorted string to be return from it. It won't compile, and it even says that "binarySort is not a member of Others".
Is it something that's not allowed to do?
This is the Class File
#include "Others.h"
#include <iostream>
using namespace std;
char Others::inputCheck(char guess) {
while (!isalpha(guess)) { //If the inputs are not alphabets, keep looping.
cout << "Enter again: ";
cin >> guess;
}
cin.ignore(1, '\n');
guess = tolower(guess);
return guess;
}
string Others::binarySort(string sampleText) {
//Bubble sorting the string. (Copy pasted)
for (int i = 0; i < sampleText.length(); i++)
{
for (int j = i + 1; j < sampleText.length(); j++)
{
if (sampleText[i] > sampleText[j]) //if previous has bigger ascii value than next,
{
//swapping the prev and next characters
char temp = sampleText[i];
sampleText[i] = sampleText[j];
sampleText[j] = temp;
}
}
}
return sampleText;
}
This is the Header File.
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
class Others
{
public:
char inputCheck(char guess); //Function to check if inputs are alphabets.
string binarySort(string sampleText);
};
#endif
The main function.
#include <iostream>
#include "Others.h"
using namespace std;
int main() {
string sortedText, sampleText = "toibeawn";
Others sorter;
sortedText = sorter.binarySort(sampleText);
cout << "Text after sorted:\n";
cout << sortedText;
return 0;
}
The sort works if it's not used for a function.
The error output:
1>Sorting_test.cpp
1>Others.cpp
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,7): error C2039: 'string': is not a member of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ types (compiler internal)(368): message : see declaration of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C3646: 'binarySort': unknown override specifier
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,24): error C2059: syntax error: '('
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,30): error C2039: 'string': is not a member of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\predefined C++ types (compiler internal)(368): message : see declaration of 'std'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(9,48): error C2238: unexpected token(s) preceding ';'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.cpp(16,21): error C2039: 'binarySort': is not a member of 'Others'
1>C:\Users\yap_2\source\repos\Sorting\Sorting_test\Others.h(5): message : see declaration of 'Others'
1>Generating Code...
1>Done building project "Sorting_test.vcxproj" -- FAILED.
You include "Others.h" prior to declaring using namespace std which results in string being unrecognized in the included header.
Add using namespace std; in the header "Others.h" and the error will be gone.
Generally, it is a bad practice to have using namespace std in headers and you'd better just write std::string.
Everything is fine. Require changes a simple change.
in your header include this header : #include and use std namespace.
So after changes, your header looks like
#ifndef OTHERS_HEADER
#define OTHERS_HEADER
#pragma once
#include <iostream>
using namespace std;
The main problem is, in your header the string is not resolved due to the lake of string header and std namespace.
Note: Always try to include language headers first and then your custom headers.
std::string binarySort(std::string sampleText)
Needed a namespace

declaration of ‘args’ as array of references Error [duplicate]

This question already has answers here:
Why are arrays of references illegal?
(14 answers)
Closed 8 years ago.
I am newbie in c++ boost , I having a program trying to compile it

#include "Program.h"
#include <boost/asio/io_service.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/udp.hpp>
namespace ConsoleApp
{
void Main(std::wstring& args[])
{
.
.
}
}
the error appear is
Program.cpp:11:31: error: declaration of ‘args’ as array of references
void Main(std::wstring& args[])
anyone here can help me , is this code error ?
thanks
The error is pretty much saying everything. std::wstring& args[] is array ([]) of wstring (std::wstring) references (&). You cannot have array of references - see Why are arrays of references illegal?.
Note: you're coding in C++, main function should be following:
int main(int argc, char *argv[])
{
// Your code
return 0;
}
EDIT:
And AFAIK main function cannot be in any namespace.
Also, there is one more problem with your code - even if we could create array of references, there is not stored information about length of the array. You couldn't use it except first element!
Anyway, you can do following (replaced wstring with string because I'm lazy):
#include <vector>
#include <string>
namespace ConsoleApp
{
void Main(std::vector<std::string> &args)
{
}
}
int main(int argc, char *argv[])
{
std::vector<std::string> args;
args.resize(argc);
for(int i = 0; i < argc; ++i)
{
args[i] = argv[i];
}
ConsoleApp::Main(args);
return 0;
}

Why the compiler outputs the following error codes: C2143, C2238, C2501

C2143: syntax error : missing ';' before '<'
I might be quite rusty in C++ since I sincerely don't know the reason of such errors.
The code is actually quite simple. (VS2003)
#include <vector>
class store
{
public:
vector<int>storage;
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Because you need to add std:: in front of vector:
std::vector<int>storage;
The vector class is inside the std namespace.
Or just add
using namespace std;
which is highly NOT recommended, especially for header files.