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
Related
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?
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?
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 3 years ago.
Improve this question
Sorry for inconvenience, I don't know much about C++, i am facing this error while running on LINUX. Two errors in last two lines, and the error is the same. What am i doing wrong?
Thanks!
#define AOCL_ALIGNMENT 64
void *ptr = NULL;
posix_memalign(&ptr, AOCL_ALIGNMENT, 128);
free(ptr);
You can't call function outside of a scope in C/C++.
You have to call them from a function:
#define AOCL_ALIGNMENT 64
int main() {
void *ptr = NULL;
posix_memalign(&ptr, AOCL_ALIGNMENT, 128);
free(ptr);
}
You have to put your code into a function, it cannot run standalone:
#include <cstddef>
#define AOCL_ALIGNMENT 64
int main (void) {
void *ptr = NULL;
posix_memalign(&ptr, AOCL_ALIGNMENT, 128);
free(ptr);
}
Without the code being inside the main function, I am getting this errors then (speficially for Lundin):
$ g++ -O2 g.c -o g -Wall
g.c:5:15: error: expected constructor, destructor, or type conversion before ‘(’ token
posix_memalign(&ptr, AOCL_ALIGNMENT, 128);
^
g.c:7:5: error: expected constructor, destructor, or type conversion before ‘(’ token
free(ptr);
^
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.
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.