visual c++ error C3867 - c++

I've had a lot of problems with this code and i've fixed most of them but i dont know whats wrong here.
1>------ Build started: Project: molar mass, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\heliz_000\documents\visual studio 2013\projects\molar mass\molar mass\source.cpp(54): error C3867: 'std::basic_ifstream<char,std::char_traits<char>>::close': function call missing argument list; use '&std::basic_ifstream<char,std::char_traits<char>>::close' to create a pointer to member
1>c:\users\heliz_000\documents\visual studio 2013\projects\molar mass\molar mass\source.cpp(55): error C3867: 'std::basic_ofstream<char,std::char_traits<char>>::close': function call missing argument list; use '&std::basic_ofstream<char,std::char_traits<char>>::close' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
That is the build output for visual studio 2013
#include <iostream>
#include <iomanip>
#include <fstream>
#include <Windows.h>
using std::basic_ofstream;//Thought it would help
using namespace std;
using std::basic_ifstream;//Thought it would help
using std::char_traits;//Thought it would help
/**
* Author: Alex M.
* Date: 3/12/2015
* Desc: Calculates the molar
* mass of compounds,
* elements and
* substances.
*/
void nor();
void tut();
void calc();
int i;
double m;
ifstream inFile;//Is this how to do if/ofstream?
ofstream outFile;
int main()
{
char ele;
bool a;
a = true;
if (i<1)
tut();
nor();
while (a = true)
{
cout << "Enter your compound or type 'Help': ";
cin >> ele;
calc();
while (ele != '\n')//This part is unfinished but seems to work
{
}
}
system("pause");
}
void nor()//This is where i think the problem is
{
inFile.open("runs.dat");
outFile.open("runs.dat");
inFile >> i;
i++;
outFile << i;
inFile.close;
outFile.close;
}
void tut()
{
cout << "Enter your equation with each " << endl << "element seperated by a space." << endl;
cout << "Example: HCl -> H Cl" << endl;
cout << "If theres more than one ion of each " << endl << "element per equation, enter that " << endl << "ion as many time as it appears." << endl;
cout << "Example: NaSO4 -> Na S O O O O" << endl;
system("pause");
}
I've already looked all over forums but no one else seems to be having my problem.
I commented all over the code with where i think the problem is.
I'm very new to C so please don't judge my simplistic coding skills.

In the error message where it says:
source.cpp(54)
source.cpp(55)
that means those errors are on line 54 and 55 respectively. You'll find that those lines are:
inFile.close;
outFile.close;
You probably meant to call those functions:
inFile.close();
outFile.close();
It's an error to mention the name of a member function in that way if you are not calling the function (and function calls require parentheses).
However, a better design would be to make inFile and outFile be local objects inside the function where you are using them. Then the files will automatically be closed when those objects go out of scope.

Related

C++ Guessing Game Error

I do not know how to declare "random" in the parentheses for "int main()," and need help. (I am a beginner in C++)
Please take a look at my code, try it out, and please notify me with an answer when you think you know how to solve this problem. It'd mean a lot to me. Thanks! Meanwhile, I will keep trying to solve the problem myself as well.
Note: I am using Code::Blocks if you want to be specific.
The error is on Line 7/9 of my code.
Here is my updated code below:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
int rn = random() % 21; // generates a random int from 0 to 20
// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;
while (u != rn) // Calculates the answer that you give
{
// If the user's number is greater than the random number
// the program will let you know it's too large
if (u > rn)
{
cout << "You guessed too big!" << endl;
}
// On the other hand, if the user guesses to small
// the program will tell them that it's too small
else if (u < rn)
{
cout << "You guessed too small!" << endl;
}
// If the user does not get the right number, the program
// will tell the user to guess again
cout << "Please guess again :" << endl;
cin >> u;
}
// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}
Here's the updated compiler error:
||=== Build: Debug in Guess The Number (compiler: GNU GCC Compiler) ===|
C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp||In function 'int main()':|
C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp|12|
error: 'randomize' was not declared in this scope|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Remove the semicolon near main, the compiler is telling you exactly what the issue is:
int main ();
Should be
int main ()
Your code will also not compile even after fixing this because you have not declared the std namespace. You can put this line at the top for now using namespace std; but it is bad practice. You should declare it manually using the scope resolution operator.
And a number of other issues as already mentioned in the comments above, make sure to read the compiler output thoroughly because it tells you what line is causing the issue.
Your code should look like:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
int rn = random() % 21; // generates a random int from 0 to 20
// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;
while (u != rn) // Calculates the answer that you give
{
// If the user's number is greater than the random number
// the program will let you know it's too large
if (u > rn)
{
cout << "You guessed too big!" << endl;
}
// On the other hand, if the user guesses to small
// the program will tell them that it's too small
else if (u < rn)
{
cout << "You guessed too small!" << endl;
}
// If the user does not get the right number, the program
// will tell the user to guess again
cout << "Please guess again :" << endl;
cin >> u;
}
// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}
Someone else got to it. There are no semicolons after signatures to methods like main().
One other thing not mentioned, I'm guessing you want
while (u != rn)
Also, be careful of the difference in "=" and "==".
BTW -- Welcome to C++!!!
a little more portable version (doesn't use conio.h) which lets the computer play against himself:
#include <iostream>
#include <cstdlib>
#include <ctime>
int get_random_in_range(int min, int max)
{
return std::rand() % (max - min) + min;
}
// returns 0 if user guessed right, negative value if user
// guessed too small, positive if user guessed too big
int check_user_guess(int guess, int my_secret)
{
return guess - my_secret;
}
int main ()
{
int my_guess = get_random_in_range(1, 10);
std::cout << "I think of " << my_guess << std::endl;
std::cout << "Please guess my number: ";
int user_guess = get_random_in_range(1, 10);
std::cout << user_guess << std::endl;
while (check_user_guess(user_guess, my_guess) != 0)
{
std::cout << "You guessed " << user_guess << std::endl;
if (check_user_guess(user_guess, my_guess) > 0)
{
std::cout << "You guessed too big!" << std::endl;
}
else if (check_user_guess(user_guess, my_guess) < 0)
{
std::cout << "You guessed too small!" << std::endl;
}
std::cout << "Please guess again: ";
user_guess = get_random_in_range(1, 10);
std::cout << user_guess << std::endl;
}
std::cout << std::endl << "You guessed it right!";
}
try it here: http://coliru.stacked-crooked.com/a/5bf0b9201ef57529

Undefined Reference to Linked List Print Function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I have a small program I have written that accepts user input and creates a linked list. I am encountering an error when building that seems to relate to the passing of the HeadPointer into the print function. The error is as follows:
undefined reference to `PrintList(PetData*)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
If anyone could point(put not intended) me in the right direction or even explicitly give me a fix I would really appreciate it.
Source Code:
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
struct PetData{
int IdNumber;
string PetType;
PetData *link;
};
PetData * HeadPointer=NULL;
void InsertItem(int, string, PetData*);
void PrintList(PetData*);
int main(){
int init_IdNumber;
string init_PetType;
PetData * CRP = NULL;
char cflag = 'Y';
while (toupper(cflag) == 'Y'){
cout << "Please Enter an ID Number: " << endl;
cin >> init_IdNumber;
cout << "Please Enter a Pet Type: " << endl;
cin >> init_PetType;
CRP = new PetData;
InsertItem(init_IdNumber,init_PetType,CRP);
HeadPointer=CRP;
cout << "Would you like to enter another? (Y/N)" << endl;
cin >> cflag;
};
PrintList(HeadPointer);
return 1;
};
void InsertItem(int init_IdNumber, string init_PetType, PetData* CRP){
CRP->PetType = init_PetType;
CRP->IdNumber = init_IdNumber;
CRP->link = HeadPointer;
};
void PrintList(){
while(HeadPointer != NULL){
cout << HeadPointer->PetType << "\t" << HeadPointer->IdNumber << endl;
HeadPointer = HeadPointer->link;
}
};
Before anyone brings it up, this is not a homework assignment. It was something my professor suggested we create independently in preparation for a future midterm assignment.
Thanks!
The function declaration and definiton is not matching.
The prototype of the function is
void PrintList(PetData*); and the definition is void PrintList(). This is why it is not matching.
There is the corrected code
void PrintList(PetData* HeadPointer){
while(HeadPointer != NULL){
cout << HeadPointer->PetType << "\t" << HeadPointer->IdNumber << endl;
HeadPointer = HeadPointer->link;
}

attempting to reference a deleted function

I'm trying to learn about the fstream class and I'm having some trouble. I created a couple of txt files, one with a joke and the other with a punchline (joke.txt) and (punchline.txt) just for the sake of reading in and displaying content. I ask the user for the file name and if found it should open it up, clear the flags then read the content in. but I cant even test what it reads in because I'm currently getting errors regarding a deleted function but I don't know what that means
error 1:
"IntelliSense: function "std::basic_ifstream<_Elem, _Traits>::basic_ifstream(const std::basic_ifstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 818 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\fstream") cannot be referenced -- it is a deleted function
the second error is the exact same but for the 2nd function (displayLastLine())
and error 3:
Error 1 error C2280: 'std::basic_ifstream<char,std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char,std::char_traits<char>> &)' : attempting to reference a deleted function
and here's my code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);
int main()
{
string fileName1, fileName2;
ifstream jokeFile, punchlineFile;
// Desribe the assigned project to the User
cout << "This program will print a joke and its punch line.\n\n";
cout << "Enter the name of the joke file (ex. joke.txt): ";
cin >> fileName1;
jokeFile.open(fileName1.data());
if (!jokeFile)
{
cout << " The file " << fileName1 << " could not be opened." << endl;
}
else
{
cout << "Enter name of punch line file (ex. punchline.txt): ";
cin >> fileName2;
punchlineFile.open(fileName2.data());
if (!punchlineFile)
{
cout << " The file " << fileName2 << " could not be opened." << endl;
jokeFile.close();
}
else
{
cout << endl << endl;
displayAllLines(jokeFile);
displayLastLine(punchlineFile);
cout << endl;
jokeFile.close();
punchlineFile.close();
}
}
// This prevents the Console Window from closing during debug mode
cin.ignore(cin.rdbuf()->in_avail());
cout << "\nPress only the 'Enter' key to exit program: ";
cin.get();
return 0;
}
void displayAllLines(ifstream joke)
{
joke.clear();
joke.seekg(0L, ios::beg);
string jokeString;
getline(joke, jokeString);
while (!joke.fail())
{
cout << jokeString << endl;
}
}
void displayLastLine(ifstream punchline)
{
punchline.clear();
punchline.seekg(0L, ios::end);
string punchString;
getline(punchline, punchString);
while (!punchline.fail())
{
cout << punchString << endl;
}
}
You do call a deleted function, being the copy constructor of the class std::ifstream.
If you take a look at the reference you notice, that the copy constructor is not allowed.
so instead of using:
void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);
you should work with calls by reference:
void displayAllLines(ifstream& joke);
void displayLastLine(ifstream& punchline);
Using a reference will behave just like calling the method with a copy, but in fact you are operating on the original object instead of a new copy-constructed object. Keep that in mind for further use of the call by reference.

Errors when linking and compiling C++ files using TextPad/G++, possibly (probably) just syntax?

This very well could be a syntax error on my part since I am rather new with using multiple files and structs in C++ (in particular, passing structs to functions). Here are the three files:
main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "common.h"
using namespace std;
void honorStatus(int, student studentList[]);
int main(void)
{
int header;
string filename;
ifstream inputFile;
student studentList[MAX_STUDENTS];
// Get filename from user and try to open file
cout << "Please enter a filename: ";
cin >> filename;
inputFile.open(filename.c_str());
// If file cannot be opened, output error message and close program
if (inputFile.fail())
{
cout << "Input file could not be opened. Please try again." << endl;
return 1;
}
// Get header number from file. If header is larger than max number
// of students, error is output and program is closed
inputFile >> header;
if (header > MAX_STUDENTS)
{
cout << "Number of students has exceeded maximum of " << MAX_STUDENTS
<< ". Please try again." << endl;
return 1;
}
// Read file information (student ID, hours, and GPA) into struct array
for (int i = 0; i < header; i++)
{
inputFile >> studentList[i].ID >> studentList[i].hours >> studentList[i].GPA;
}
// Close the file
inputFile.close();
// Calls function honorStatus
honorStatus(header, studentList);
return 0;
}
functs.cpp:
#include <iostream>
#include "common.h"
using namespace std;
// Function to determine classification and honors society eligibility requirements
// of each student, outputting this information and the number of students eligible
void honorStatus(int fheader, student fstudentList[])
{
int cnt = 0;
for (int i = 0; i < fheader; i++)
{
if (fstudentList[i].hours < 30)
{
cout << "Student #" << fstudentList[i].ID << " is a freshman with GPA of "
<< fstudentList[i].GPA << ". Not eligible." << endl;
}
else if (fstudentList[i].hours > 29 && fstudentList[i].hours < 60)
{
if (fstudentList[i].GPA >= 3.75)
{
cout << "Student #" << fstudentList[i].ID << " is a sophomore with GPA of "
<< fstudentList[i].GPA << ". Eligible." << endl;
cnt++;
}
else
{
cout << "Student #" << fstudentList[i].ID << " is a sophomore with GPA of "
<< fstudentList[i].GPA << ". Not Eligible." << endl;
}
}
else if (fstudentList[i].hours > 59 && fstudentList[i].hours < 90)
{
if (fstudentList[i].GPA >= 3.5)
{
cout << "Student #" << fstudentList[i].ID << " is a junior with GPA of "
<< fstudentList[i].GPA << ". Eligible." << endl;
cnt++;
}
else
{
cout << "Student #" << fstudentList[i].ID << " is a junior with GPA of "
<< fstudentList[i].GPA << ". Not eligible." << endl;
}
}
else
{
if (fstudentList[i].GPA >= 3.25)
{
cout << "Student #" << fstudentList[i].ID << " is a senior with GPA of "
<< fstudentList[i].GPA << ". Eligible." << endl;
cnt++;
}
else
{
cout << "Student #" << fstudentList[i].ID << " is a senior with GPA of "
<< fstudentList[i].GPA << ". Not eligible." << endl;
}
}
}
cout << "\nTotal number of students eligible for the Honor Society is " << cnt << "." << endl;
}
common.h:
// Maximum number of students allowed
const int MAX_STUDENTS = 10;
// Struct for student info
struct student
{
int ID;
int hours;
float GPA;
};
When using TextPad/G++, I get the following error:
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccxq9DAh.o:p7b.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccLa96oD.o:test.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
collect2: error: ld returned 1 exit status
When using an online C++ compiler (CompileOnline), I get:
/tmp/ccIMwHEt.o: In function `main':
main.cpp:(.text+0x1cf): undefined reference to `honorStatus(int, student*)'
collect2: error: ld returned 1 exit status
I wasn't able to find a guide on how to set up TextPad/G++ to compile and link multiple files, but my instructor gave a short set of instructions that I followed. Here is how it's set up:
So this could a two-parter question (how do I set up TextPad to correctly compile/link files? why is my honorStatus() function undefined in main.cpp?) or it could just be that my syntax is wrong. I'm honestly not sure. Sorry if this is a bit long; I wanted to include as much detail as possible. Any help is greatly appreciated.
The problem is that you are compiling "*.cpp" all together. Given this
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccxq9DAh.o:p7b.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccLa96oD.o:test.cpp:(.text+0x0): multiple definition of `main'
/cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o:p5.cpp:(.text+0x0): first defined here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../../i686-pc-cygwin/bin/ld: /cygdrive/c/Users/Korina/AppData/Local/Temp/ccmtzOP2.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
collect2: error: ld returned 1 exit status
we can see that the the compiler has been trying to combine p5.cpp, p7b.cpp and test.cpp into one executable (possibly other .cpp files too).
You need to actually tell the compiler exactly which files you want to build together to one program. E.g.
g++ main.cpp functs.cpp -o main.exe
(I would suggest also adding -Wall -Wextra -Werror to the compile line, as that allows the compiler to detect the small mistakes that aren't strictly errors, but where you probably got something wrong)
From the linker output you can see that main function is found in these files: p7b.cpp, p5.cpp and test.cpp. As there's no main.cpp file listed in the linker output, I guess that current directory is setup to be where p7b.cpp and other files are located.
Try to change Initial Folder to be where your main.cpp file is set (something like /cygdrive/c/Users/Korina/programming/). Also, remove all unrelevant files from that directory, as you're compiling all cpp files.
The error message is clear enough. Your project contains the following files
p7b.cpp, p5.cpp, test.cpp
where in each file there is defined function main. Put a place in order with your project files.
As for the error message when you use the inline compiler then it seems module functs.cpp is not included in the project. So the compiler does not see the function definition.

Syntax Query, calling modules in Visual C++

I have tried to adapt my knowledge of modularity to Visual C++ however, upon what seems to be an endless search scouring for syntax, I simply can't get this right. Basically in this code, the menu is called first, once the user enters their choice (only coded option 1 thus far) to return that value to the main, which then steps into the if statement and calls fahrenheit. I am requesting the syntax for passing by reference, I know C#'s syntax for this, but not Visual C++
Here's the code.
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void Celsius()
{
}
void fahrenheit()
{
cout << "Success!" << endl; //....Outputs this just to see if the module is being called properly.
}
int menu(int Mystring) //....I was testing this syntax to pass the variable.
{
cout << "What would you like to do : " << endl;
cout << "1) Fanreheit to Celsius" << endl;
cout << "2) Celsius to Fahrenheit" << endl;
cout << "Choice : " ;
cin >> Mystring;
return Mystring;
}
int main()
{
int celsius = 0;
int fahrenheit = 0;
int Mystring = 0;
menu(Mystring); //....Testing this syntax to pass Mystring to menu.
if (Mystring == 1) //....I was hoping the menu would return Mystring as value = 1.
{
fahrenheit(); //.......I want this to call fahrenheit module if Mystring = 1
}
}
The "things" you're talking about aren't called modules, but functions. That's a pretty big difference and I think you should know it, since you won't understand nearly any article without that knowledge.
That being cleared, the problem in your code is, that you pass the variable by value (int menu(int Mystring)), while - in order to change it inside the function - you need to pass it by reference or pointer:
int menu(int &Mystring)
There are plenty of articles about functions in C++. You should check them out probably.