Function not recognizing the parameters

I have a function void subtract(Interval I); that will not recognize the parameter,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void subtractBuildings(double borderLength)
{
  IntervalSet highway(0.0, borderLength);
  double x1, x2;
  cin >> x1 >> x2;
  while (x1 <= x2)
    {
      Interval building (x1, x2);
      highway.subtract(building);
      cin >> x1 >> x2;
    }
  double sum = highway.sum();
  cout << "The total planting length is "
       << setiosflags(ios::fixed) << setprecision(1) << sum << endl;
}


It keeps giving me the errors:
error: 'Interval' is not a type|
error: 'Interval' is not a type|
In function 'void subtractBuildings(double)':|
error: no matching function for call to 'IntervalSet::subtract(Interval&)'|
note: candidate is:|
note: void IntervalSet::subtract(int)|
note: no known conversion for argument 1 from 'Interval' to 'int'|

There is a header file that creates the interval and that works fine. I don't understand why it will not take the parameter and what its talking about about converting to int. I have an subtract function in the cpp file void IntervalSet::subtract(Interval I) but it still doesn't recognize the parameter.
Last edited on
closed account (28poGNh0)
maybe It is better if you give us the whole code source
So where and how did you define the Interval type?

There is a header file that creates the interval and that works fine

Did you include that header before you tried to use that type?

I dpn't understand why it will not take the parameter and what its talking about about converting to int.


How was subtract() defined?

I'm trying not to post too much because it is an assignment, but these are the given files that I cannot change.
This is the main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iomanip>
#include <iostream>
#include "intervalSet.h"
#include "interval.h"

using namespace std;


void subtractBuildings(double borderLength)
{
  IntervalSet highway(0.0, borderLength);
  double x1, x2;
  cin >> x1 >> x2;
  while (x1 <= x2)
    {
      Interval building (x1, x2);
      highway.subtract(building);
      cin >> x1 >> x2;
    }
  double sum = highway.sum();
  cout << "The total planting length is "
       << setiosflags(ios::fixed) << setprecision(1) << sum << endl;
}





int main ()
{
  double L;
  cin >> L;
  subtractBuildings (L);
  return 0;
}


This is the Interval Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef INTERVAL_H
#define INTERVAL_H

#include <iostream>


class Interval {
private:
  double low;
  double high;

public:
  // Construct a new interval with the indicated bounds
  Interval (double l, double h);

  // Get the lower bound of this interval
  double getLower() const {return low;}

  // Get the upper bound of this interval
  double getUpper() const {return high;}


  // Test to see if this interval overlaps with another
  bool overlaps (Interval x) const;


  // Get the portion of this interval that lies below x. For
  // example:
  //   Interval w (-10.0, 10.0);
  //   Interval z = w.below(5.0);
  // z should be (-10.0..5.0)
  Interval below (double x) const;


  // Get the portion of this interval that lies above x. For
  // example:
  //   Interval w (-10.0, 10.0);
  //   Interval z = w.above(5.0);
  // z should be (5.0..10.0)
  Interval above (double x) const;

  // Test to see if this interval is empty
  bool empty () const
  {
    return high <= low;
  }

  // Print an interval
  void print (std::ostream& out) const;

};

#endif 


This is the Interval cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef INTERVAL_H
#define INTERVAL_H

#include <iostream>


class Interval {
private:
  double low;
  double high;

public:
  // Construct a new interval with the indicated bounds
  Interval (double l, double h);

  // Get the lower bound of this interval
  double getLower() const {return low;}

  // Get the upper bound of this interval
  double getUpper() const {return high;}


  // Test to see if this interval overlaps with another
  bool overlaps (Interval x) const;


  // Get the portion of this interval that lies below x. For
  // example:
  //   Interval w (-10.0, 10.0);
  //   Interval z = w.below(5.0);
  // z should be (-10.0..5.0)
  Interval below (double x) const;


  // Get the portion of this interval that lies above x. For
  // example:
  //   Interval w (-10.0, 10.0);
  //   Interval z = w.above(5.0);
  // z should be (5.0..10.0)
  Interval above (double x) const;

  // Test to see if this interval is empty
  bool empty () const
  {
    return high <= low;
  }

  // Print an interval
  void print (std::ostream& out) const;

};

#endif 


I then created an IntervalSet header & cpp #including the interval header.
I declared the function in the header
void subtract(Interval I);
and then called it in the cpp
1
2
void IntervalSet::subtract(Interval I)
{
You need to post the contents of your intervalSet header and source file.

Really the smallest possible complete program that illustrates your problem would be helpful. You don't need to show everything. Just what is required for us to be able to compile the program to see the problems for ourselves.

//
Last edited on
Since you are using a vector<Interval> you probably don't need to inherit the Interval class. Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef INTERVALSET_H
#define INTERVALSET_H

#include <iostream>
#include <vector>
#include "interval.h"


class IntervalSet
{
private:
    std::vector<Interval> seq;
//    IntervalSet Interval();
public:

    /*Construct a new interval set containing a single starting interval (which the constructor will received as a parameter.*/
    IntervalSet(double l, double h);

    // Subtract an interval I from the interval set, leaving behind only those portions of the original interval set that did not overlap with I
    // example:
    void subtract(Interval I);

    double sum();
};

#endif 


1
2
3
4
5
/*Construct a new interval set containing a single starting interval (which the constructor will received as a parameter.*/
IntervalSet::IntervalSet (double l, double h)  : seq(1,Interval(l,h)) 
{
   // Blank body.
}


Ok, but that still does not change the errors. The subtract function still isn't taking in the parameter. Or are you saying I should call the subtract function in
1
2
3
4
5
6
/*Construct a new interval set containing a single starting interval (which the constructor will received as a parameter.*/
IntervalSet::IntervalSet (double l, double h)  : seq(1,Interval(l,h)) 
{
   // Blank body.
}
?
closed account (28poGNh0)
the header interval.h is matching with interval.cpp
I didn't have any problem with the parameters to the function, but the function does seem to have several errors.

It should look something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*Subtract an interval I from the interval set, leaving behind only those portions of the original interval set that did not overlap with I.*/
void IntervalSet::subtract(Interval I)
{
    double startI = I.getLower() ; //start of x  // Actually call the member functions of I.
    double endI = I.getUpper() ; //end of x  
    for(int i = 0; i <= seq.size(); i++)
    {
        while(!seq[i].empty() && seq[i].overlaps(I))  // Note the [] instead of the ().
        {
            seq.push_back(I.below(startI));  // Use the members of I.
            seq.push_back(I.above(endI));
        }
    }

}
1
2
3
4
IntervalSet::IntervalSet (double l, double h)  : seq(1,Interval(l,h)) 
{
   // Blank body.
}


Does the above compile for you?

I get the errors,

1
2
3
4
5
6
7
In constructor 'IntervalSet::IntervalSet(double, double)':|
error: no matching function for call to 'Interval::Interval()'|
note: candidates are:|
note: Interval::Interval(double, double)|
note:   candidate expects 2 arguments, 0 provided|
note: Interval::Interval(const Interval&)|
note:   candidate expects 1 argument, 0 provided|
Last edited on
It looks like it is creating the interval and putting it into the vector. Is that correct?

Yes, exactly. This is creating 1 element in your vector and populating that vector with an Interval that is constructed using the constructor that takes two double parameters.

Nevermind, I forgot to take out something. Thank you, it compiles now. Now onto the next problem....
Everything seems fine about the way you approached the problem @jlb, but how would you get your values out of the vector? You cant just add the vector's values like you would normally.
What? What exactly do you mean by "get your values out of the vector"?

for example if I wanted to add the intervals together, i could do
double s=seq[i]+s; but if you do that with the way you did it, you should get errors. So i guess the question should have been is there anyway to perform arithmetic operations on Vector<Interval> seq? Im not a very good programmer so if i'm completely wrong just let me know.
So i guess the question should have been is there anyway to perform arithmetic operations on Vector<Interval> seq?

Yes. If you overload the proper operators in the class you use in the vector you can do what you are trying to do.

If you have questions I suggest you open your own topic with your code and ask your specific questions about your code, instead of asking in someone else's topic.

Topic archived. No new replies allowed.