user3123287
user3123287

Reputation: 11

Error C2679 - C++ - No Operator Found

I was wondering what was causing this error, I am trying to get it to import data from an external file and use it.

#include <iostream>
#include <string>
#include <fstream>
#include <istream>

using namespace std;

int main()
{
    int gardenn();
    // Here you can ask your questions about dimensions and calculate cost
    float lawncost(string item, float unitCost);

    int main();
    {
        // Read your items and costs in a while loop
        string item;
        float  unitCost;
        ifstream catalog;
        catalog.open("priceCatalog.txt");
        while (catalog >> ("lawn") >> unitCost);
        {
            lawncost(item, unitCost);
        }
        cout << lawncost;
    }
    return 0;
}

When I run it I get this error:

Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [5]' (or there is no acceptable conversion) c:\users***\documents\visual studio 2012\projects\project10\project10\source.cpp 20 1 Project10 Error 2 error C1903: unable to recover from previous error(s); stopping compilation c:\users**\documents\visual studio 2012\projects\project10\project10\source.cpp 20 1 Project10

Upvotes: 0

Views: 737

Answers (2)

Caesar
Caesar

Reputation: 9843

What you have is not a valid C++ code. Here is some issues I can see in it.

  1. int gardenn(); This is not an int, but a function declaration that returns an int and accepts no parameters.
  2. int main(); Not sure what you are doing here. If you are declaring an int then you have the same issue with number 1. If you are declaring a nested function then C++ doesn't support that.
  3. while (catalog >> ("lawn") >> unitCost); You have a semicolon at the end of the while loop and so the scope underneath it is not part of the while loop.
  4. while (catalog >> ("lawn") >> unitCost); Not sure what you are doing with catalog >> ("lawn") because that is not a valid C++ code. ("lawn") is a const char* and so you can't write to that.
  5. float lawncost(string item, float unitCost); You are declaring the function but never giving it a body. You will need to give it a body so that the linker can link to it and so that your code can use that function. You also need to write the function outside of the main function because C++ doesn't support nested functions.
  6. cout << lawncost; lawncost is a function, but you are not calling that function and so instead of the return value being printed as you might expect, what will be printed is its memory location.

Upvotes: 3

111111
111111

Reputation: 16158

while (catalog >> ("lawn") >> unitCost);
{
    lawncost(item, unitCost);
}

Should probably be:

std::string lawn;
while(catalog >> lawn >> unitCost) {
    if(lawn == "lawn")
        std::cout << lawncost(item, unitCost) << '\n';

}

Note I also removed the ; from the end of the while predicate.

Upvotes: 1

Related Questions