Question:
How can I fix my c++ program?
a-nerd
2010-11-01 16:11:40 UTC
I started programming with Java, which is okay but a bit slow. I decided to learn c++ from cplusplus.com, and am now trying my own program. I am using Eclipse for C++ on ubuntu 10.10 with the GCC/G++ linux compiler installed.
I am getting an error when I try to spread a program out in several files. Specifically, I get the error whenever I attempt to call a member method of my "printer" object that returns void and takes no arguments. The error displays when compiling.
Here's the error:

**** Build of configuration Debug for project ConsoleLanguage ****

make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp: In function ‘int main(int, const char**)’:
../main.cpp:17: error: request for member ‘print’ in ‘prnt’, which is of non-class type ‘printer()’
make: *** [main.o] Error 1



here's main.cpp:
/*
* main.cpp
*
* Created on: Nov 1, 2010
* Author: *****
*/

#include
#include "printer.h"
using namespace std;

int main(int argc, const char** argv){
//First hello world!
cout << "Hello, World!" << endl;
//now do it with a different method:
printer prnt();
prnt.print();
return 0;
}


Here's printer.h:

* printer.h
*
* Created on: Nov 1, 2010
* Author: *****
*/

#ifndef PRINTER_H_
#define PRINTER_H_

class printer {
public:
printer();
virtual ~printer();
void print();
};

#endif /* PRINTER_H_ */


here's printer.cpp:
/*
* printer.cpp
*
* Created on: Nov 1, 2010
* Author: *****
*/

#include "printer.h"

printer::printer() {
// TODO Auto-generated constructor stub

}

printer::~printer() {
// TODO Auto-generated destructor stub
}

printer :: print(){
std::cout << "Hello, World!..." <}


Where did I mistype?
Three answers:
peteams
2010-11-01 16:37:15 UTC
Visual Studio gives a similar error when it tries to compile your code:



error C2228: left of '.print' must have class/struct/union



If you comment out the line it complains about you get instead:



warning C4930: 'printer prnt(void)': prototyped function not called (was a variable definition intended?)



So that's your problem



printer prtn();



is seen as a prototype for a function taking no parameters and returning printer. What you want is:



printer prtn;



(You also need a void in your implementation of void printer::print()!)
2016-10-14 02:57:54 UTC
I see this situation each and every few days here on YA and the main consumer-friendly errors are: misusing information types on your cas you get int coeff. yet bypass then to a function that demands double! do no longer do this. you may desire to forged them: getCalc( (double) a, (double) b, (double) c ); is the least puzzling restore
blackxzero
2010-11-01 16:22:24 UTC
printer prnt();



should be:



printer prnt;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...