Why is my program giving ambiguous reference to wstring error? [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
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.

Related

How to move address of a string to a function in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I was trying to send the address of a string to a function in C++ I'm but facing problems like in the code below:
#include<iostream>
#include<string>
using namespace std;
void try2(string *a){
cout<<a[2];
}
int main(){
string a;
getline(cin,a);//string length is greater than 10
try2(&a);
}
I am doing the same that we do for string in C. But I don't have that much idea about C++. Please suggest and describe how string behaves in C++.
You can pass a string into a function as an argument and take a string reference as a parameter in the function. But you need to understand that a string isn't an array, it is an object of the string class. The reason you can use indexing on a string is because the [] operator is overloaded for the string class.
What you were doing above is passing a pointer. For it to work the way you had done it your function would have to dereference the pointer:
#include<iostream>
#include<string>
using namespace std;
void try2(string *a){
cout<<(*a)[2];
return;
}
int main(){
string a;
getline(cin,a);//string length is greater than 10
try2(&a);
}
This would allow you to print the third character of the string object passed by reference to the function.
Note: don't use try as a function name as it is already reserved by many languages for the try-catch block syntax and can confuse the compiler.
You are accessing an single string as array of strings, which it is not. If you are trying to access the 2nd char of the string you have to dereference it first and then access the char like this:
#include<iostream>
#include<string>
using namespace std;
void try(string *a){
cout<<(*a)[2];
}
int main(){
string a;
getline(cin,a);//string length is greater than 10
try(&a);
}
This is because in c++ arrays and pointer are very close to each other.
*a and a[0] are almost the same which means using a[2] on a pointer isd the same as using *(a+2).
You have to keep in mind that you are not using an c-string here bit an c++-std::string which is an object and not an simple memory structure like in c.
So you first have to dereference the object before you can use it's overloaded []-operator to acess it's members (chars in this case).

Need help whit c++ pointers [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 6 years ago.
Improve this question
I'm studing about c++ semantics and syntax, I really don't know what is the problem with this code, it compile but stop working. I will apreciate your help, thanks.
#include <iostream>
#include <string.h>
using namespace std;
char* func(char* M)
{
int initval = 2;
char *x= new char[10];
x="idea";
strcpy(x, M+initval);
return x;
}
int main()
{
char* x;
char s[10]= "alguna";
x= func(s);
cout << *x << endl;
return 0;
}
Before this is closed, the x="idea"; is where your problem lies. You throw away your buffer and point it to a constant value, then try to assign to it, which almost always is illegal (should always be illegal, but apparently it is compiling for you...).

Error: Initializing Argument 1 of [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
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>)

find bugs in the c++ code below [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
I have been asked to find bug in the following code
This code just prints abcdef, it seems pretty harmless to me but will appreciate any suggestions
#include <iostream>
using namespace std;
const char* append(const char* s1, const char* s2) {
string s(s1);
s += s2;
return s.c_str();
}
void foo() {
const char* total = append("abc", "def");
cout<<"total = "<<total<<endl;
}
int main() {
foo();
return 0;
}
You have a dangling pointer. s is destroyed when it goes out of scope, meaning the pointer to its internal data is no longer valid.

Creating my own std::vector in C program [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
I know how to code with C++, however this is my first time I try to use C.
I even tried to define a cVector.h and cVector.c in order to implement some of the std::vector functionality. but when I compile my code I receive the following error.
Here is same of the code:
cVector.h
#define VECTOR_INITIAL_CAPACITY 520
typedef struct {
int size; // slots used so far
int capacity; // total available slots
int *data; // array of integers we're storing
} Vector;
void vector_init( Vector *vector);
cVector.c
#include "cVector.h"
#include <stdio.h>
#include <stdlib.h>
void vector_init(Vector *vector) {
// initialize size and capacity
vector->size = 0;
vector->capacity = VECTOR_INITIAL_CAPACITY;
// allocate memory for vector->data
vector->data = malloc(sizeof(int) * vector->capacity);
}
here is the usage:
#include "cVector.h"
Vector times;
vector_init(&times);
int main{
....}
and finally error:
Ser.c:135:13: error: expected declaration specifiers or ‘...’ before ‘&’ token
You can't call a function at file scope like that. You need to move the call into a function (e.g. main).
You can't use a function outside the declaration of another function. Btw you can declare variables as globals variables but the line
vector_init(&times);
must be written inside of the main function.
If you are interested the gcc's error message it's because of he is trying to find the declaration of a new function, this is, the name of a type or ... instead.