Unable to push_back to vector [closed] - c++

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 7 years ago.
Improve this question
I have created an instance of the class however I am unable to push_back the name of the ship to the vector.
if (rows->_rows.size() != rows->rows_Size)
{
rows->_rows.push_back (ship->name); }
I get the following errors.
Error 1 error C2664: 'void std::vector<_Ty>::push_back(Berths &&)' : cannot convert parameter 1 from 'std::string' to 'Berths &&'
6 IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=Berths, _Alloc=std::allocator]" matches the argument list
argument types are: (std::string)
object type is: std::vector>
The code for the class I am trying to add the data to is:
#pragma once
class Berths;
#include "Berths.h"
#include "Ship.h"
#include <iostream>
#include <vector>
class Rows { public: Rows(void); ~Rows(void);
std::vector<Berths> _rows; int rows_Size;
bool dock(Ship ship);
bool undock(Ship ship);
};
Rows::Rows(void)
{
rows_Size = 10;
}
Any ideas as to what is causing this?
Thanks

.size() is a member function of a vector. Hence you should use rows->_rows.size() instead. It's a common mistake we all make.

Related

Getting a compilation error while using (<=>) [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 days ago.
Improve this question
New to C++ here, following outdated training videos...
Trying to compile this simple code, but getting the below error:
#include <iostream>
int main(){
auto result = (10 <=> 20) > 0;
std::cout << result << std::endl;
}
Error I get is:
prog.cc: In function 'int main()':
prog.cc:4:25: error: expected primary-expression before '>' token
4 | auto result = (10 <=> 20) > 0;
|
What am I doing wrong?

Ambiguating new declaration of 'void visualizar()' [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 2 years ago.
Improve this question
I am new in C++ and i have a little error in this program "Hello world"
//ejemplo funciones definidas por el usuario
#include<iostream>
using namespace std;
int visualizar();
int main()
{
visualizar();
return 0;
}
void visualizar() //Here is the error
{
cout<<"Hola mundo guay\n";
}
Error:
C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp In function 'void visualizar()':
15 17 C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp [Error] ambiguating new declaration of 'void visualizar()'
6 5 C:\Users\lisan\OneDrive\Desktop\c++\EjemploFunciones.cpp [Note] old declaration 'int visualizar()'
What does this error mean? What caused it? How do I fix it?
In your prototype of visualizar, you declared it as
int visualizar();
However, when you defined it, you wrote
void visualizar() { ... }
Notice that the return types are different. Did you mean to use void throughout?

could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::string' [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
When I write such a code in visual studio:
void aaa()
{
std::string a = " ";
std::string b = "";
b += 1.0f + a;
}
Why do I receive such an error? Where does std::reverse_iterator come from?
error C2784: 'std::reverse_iterator<_RanIt> std::operator +(reverse_iterator<_RanIt>::difference_type,const std::reverse_iterator<_RanIt> &)': could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::string'
Indeed, that's an expected error message from Microsoft compiler.
The reason it happens is because you included <string> header which defines a few operator+ templates for different iterators:
// reverse_iterator TEMPLATE OPERATORS
template<class _RanIt> inline
reverse_iterator<_RanIt> operator+(
typename reverse_iterator<_RanIt>::difference_type _Off,
const reverse_iterator<_RanIt>& _Right)
{ // return reverse_iterator + integer
return (_Right + _Off);
}
Since there is no operator+ exists for adding floats and std::string your code won't compile. But during compilation compiler detected that above mentioned std::operator+ might be possible candidate since your float can be converted to reverse_iterator<_RanIt>::difference_type. When it tried this operator+ it wasn't able to deduce what type it should use in place of _RanIt to convert std::string to const reverse_iterator<_RanIt>& it work out.
As you noted, compiler then tries to match other versions of operator+ for other iterator types:
error C2784: 'std::_Array_const_iterator<_Ty,_Size> std::operator
+(_Array_const_iterator<_Ty,_Size>::difference_type,std::_Array_const_iterator<_Ty,_Size>)':
could not deduce template argument for
'std::_Array_const_iterator<_Ty,_Size>' from 'std::string'
error C2784: 'std::_Array_iterator<_Ty,_Size> std::operator
+(_Array_iterator<_Ty,_Size>::difference_type,std::_Array_iterator<_Ty,_Size>)':
could not deduce template argument for
'std::_Array_iterator<_Ty,_Size>' from 'std::string' ...
As a side note, in C++ you should first convert numbers to strings and then concatenate (e.g. add) them. This should work for you:
b += std::to_string(1.0f) + a;

Understanding errors in printing a Linked list in C++ [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 9 years ago.
Improve this question
I have recently worked on C, but I have started to study C++. I had a homework in to create a program that would read texts and organize data out of the imputed text. This is the last part I have left, but I don't get what's wrong with my code. This part of the problem is pretty simple, but I still don't understand what my errors are. I got used to gcc compiler which wrote mostly segmentation fault, but g++ compiler errors are different. Any tips or hints on what to pay more attention while transferring from c to c++ would be really appreciated.
This is my output errors.
-bash-3.2$ g++ -o Printfunction Printfunction.cpp
Printfunction.cpp: In function 'void Printfunction(wordList*)':
Printfunction.cpp:43: error: cannot convert 'NumberList*' to 'Numberlist*' for argument '1' to 'std::string returnlist(Numberlist*)'
Printfunction.cpp: In function 'std::string returnlist(Numberlist*)':
Printfunction.cpp:56: error: invalid use of undefined type 'struct Numberlist'
Printfunction.cpp:10: error: forward declaration of 'struct Numberlist'
Printfunction.cpp:56: error: 'to_string' was not declared in this scope
Printfunction.cpp:57: error: invalid use of undefined type 'struct Numberlist'
Printfunction.cpp:10: error: forward declaration of 'struct Numberlist'
Can you please tell me what's wrong with my code?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct NumberList
{
int line;
struct Numberlist *nextPtr;
};
struct wordList
{
string word;
int Count;
NumberList lines;
struct wordList *nextPtr;
};
void Printfunction(wordList *list);
string returnlist(Numberlist *list);
int main()
{
wordList something;
something.word = "SOMETHING";
something.Count = 55555;
something.nextPtr = NULL;
Printfunction(&something);
}
void Printfunction(wordList *list)
{
int i;
i=1;
cout<<"+----+----------------------------+-------+---------------------------------+"<<endl;
cout<<"|# | WORD | COUNT | LINES |"<<endl;
cout<<"+----+----------------------------+-------+---------------------------------+"<<endl;
while(list != NULL)
{
cout<<"|"<<left<<setw(4)<<i<<"|"<<left<<setw(28)<<list->word<<"|"<<left<<setw(7)<<list->Count<<"|"<<left<<setw(33)<<returnlist(&(list->lines))<<"|"<<endl;
cout<<"+----+----------------------------+-------+---------------------------------+"<<endl;
list = list->nextPtr;
i++;
}
}
string returnlist(Numberlist *list)
{
string final;
while(list != NULL)
{
final.append(", ");
final.append(to_string(list->line));
list = list->nextPtr;
}
final.append(".");
return final;
}
The problem is that sometimes you spell it NumberList, and sometimes you spell it Numberlist.
Any tips or hints on what to pay more attention
Case matters.

error: class ‘Software’ does not have any field named ‘ptr’ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
I am getting the following errors:
softwarew.hh: In constructor ‘Software::Software(std::string, int)’:
softwarew.hh:26:45: error: class ‘Software’ does not have any field named ‘ptr’
softwarew.hh:28:7: error: ‘ptr’ was not declared in this scope
softwarew.hh: In destructor ‘Software::~Software()’:
softwarew.hh:40:6: error: ‘ptr’ was not declared in this scope
Can someone explain why I receive these errors?
The code that causes the errors:
Software(std::string name, int revision) : ptr(software_construct(name.c_str(), revision) ) {
if(!ptr) throw std::runtime_error("no software created");
}
~Software(){
if(ptr)
software_destruct(ptr);
}
private:
struct Software_s* ptr;
Your error says
"class Software does not have any field named ptr"
Given some suitable definitions of Software_s, software_construct, and software_destruct, making sure you put the field inside the class works:
#ifndef SOFTWAREw_INCLUDED
#defINE SOFTWAREw_INCLUDED
class Software{
Software(std::string name, int revision)
: ptr(software_construct(name.c_str(), revision)) {
if(!ptr)
throw std::runtime_error("no software created");
}
~Software(){
if(ptr)
software_destruct(ptr);
}
private:
struct Software_s* ptr;
};
#endif